Did you find this useful?
Socialize it today.


JavaScript If Else Conditional Statements

Monday, April 09, 2012

When developing scripts and programs, you will reach a point in time where you need to include decision making logic, or conditional statements. Most other programming languages use a form of conditional logic, and JavaScript is no exception to that basic concept.


Types of Conditional Statements

There are various conditional statements that can be used in JavaScript coding. The three most common are the "if", "if...else", and the "if...else if...else" statements. The "if" statement is used to execute JavaScript code only if a specified condition is true.  The "if...else" statement can be used to execute a block of code if the condition is true and another block of code if the condition is false.  Finally, the "if...else if....else" statement is used to select one of many blocks of code to be executed.


If Statement

You should use the if statement to execute some code only if a specified condition is TRUE. There are two parts to an If Statement: the conditional statement and the code to be executed. The conditional statement evaluates to be either TRUE or FALSE. If the condition is TRUE, the specified block of code is executed. If the condition is FALSE, the block of code is not executed. Here is an example.


if (condition)
{
code to be executed if condition is TRUE
}


<script type="text/javascript">
<!--
if (num == 5)
{
document.write("The variable is in fact set to 5!");
}
//--> </script>


If...Else Statement

You should use the if...else statement to execute some code if a specified condition is TRUE, and execute another block of code if the condition is FALSE. Here is an example.


if (condition)
{
code to be executed if condition is TRUE
} else
{
code to be executed if condition is FALSE
}


<script type="text/javascript">
<!--
if (num == 5)
{
document.write("The variable is in fact set to 5!");
}
else
{
document.write("The variable is not set to 5!");
}
//--> </script>


If...Else If...Else Statement

The If...Else If...Else statement is an extension to the If Statement that allows you to select one of several blocks of code to be executed. Here is an example.


if (condition)
{
code to be executed if condition is TRUE
} else if (next condition)
{
code to be executed if the previous condition was FALSE, but this condition is TRUE
}
else
{
code to be executed if all previous conditions are FALSE
}


<script type="text/javascript">
<!--
if (num > 5)
{
document.write("The variable is greater than 5!");
}
else if (num > 2 && num < 5)
{
document.write("The variable is set to either 3 or 4!");
}
else
{
document.write("The variable is less than 3!");
}
//--> </script>


In this previous example, we used only one Else...If condition. However, you can have additional sets of Else...If conditions. Simply place them one after another. Always end with an Else statement.

Please help us spread the word by socializing it today!

email contact us

Did you find something wrong with the information on this page? Please take a moment to report it to us so that we can continue to improve the quality of the information on this site. Click here to report an issue with this page.



Recommended Books & Training Resources

Professional JavaScript for Web Developers JavaScript and jQuery: The Missing Manual HTML CSS and JavaScript Editor