Conditional Statements

Conditional statements are the backbone for creating any type of logic in a scripting or programming language, and the JavaScript language is no exception. Conditional statements determine what action to take based on the conditions you script. There are four ways to write conditional statements in the JavaScript language, which are described below.

Conditional statements

Use the if statement if you only want to execute a script if a condition is true. Example below shows how to use an if statement with a comparison operator to execute a script if the condition is true.

Using an if statement

var num = 10;

if(num == 5)

{

document.write("num is equal to 5");

}

Use the if...else statement if you want to execute one script if a condition is true or another script if the condition is false, as shown below.

Using an if...else statement

var num = 10;

if(num == 5)

{

document.write("num is equal to 5");

}

else

{

document.write("num is NOT equal to 5, num is: "+ num);

}

Use the if...else if...else statement if there are different scripts that should execute based on different conditions, as shown below.

Using an if...else if...else statement

var num = 10;

if(num == 5)

{

document.write("num is equal to 5");

}

else if(num == 10)

{

document.write("num is equal to 10");

}

else

{

document.write("num is: "+ num);

}

Switch statements are different than if statements in that they cannot be used to determine whether the value of a variable is greater or less than another value. Below shows an example of when it is appropriate to use a switch statement to determine what script to execute.

Using a switch statement

var num = 10;

switch(num)

{

case 5:

document.write("num is equal to 5");

break;

case 10:

document.write("num is equal to 10");

break;

default:

document.write("num is: "+num);

}

You probably noticed the use of the case clause, break statement and default clause above. These clauses and statements are critical to the switch statement. The case clause determines whether the value of switch is equal to the value of the data used in the case clause. The break statement breaks—or stops—the switch statement from executing the rest of the statement. And the default clause identifies a script that runs by default if none of the case statements are executed or if the executed case statements do not have break statements. For example, below shows how multiple case statements and the default statement can be executed if no break statements are used in the executed case statements.

Executing multiple lines of code by excluding a break

var num = 10;

switch(num)

{

case 5:

document.write("num is equal to 5");

break;

case 10:

document.write("num is equal to 10");

default:

document.write("num is: "+num);

}

The result of this script is "num is equal to 10" followed by "num is: 10." This is sometimes called switch fall-through.

As mentioned at the beginning of this section, conditional statements are the backbone of all logic in any scripting or programming language, but without functions you have a tangled mess of code.