Hosting Solutions
mobile_logo Table of Contents
seal_frontseal_back

if else

The if...else statement is used for evaluation logic. Where any expression returns a value of "true", the if...else statement will stop processing and execute code nested in that condition block. The if statement always goes on top and starts the evaluations. The if statement can also be used by itself without the else and else...if. The optional multiple else...if statements go in between the if and the else and allow you to evaluate many more conditions in that particular evaluation group. The else statement always goes on the bottom and that code executes if no conditions are met(final clause).

Syntaxif ( true ) { ... } else if ( true ) { ... } else { ... }
An if condition used by itself: var a = 5; if ( a < 10 ) { alert( a + " is less than 10" ); } if conditions used with else conditions: var a = 5; var b = 3; if ( a < b ) { alert( a + " is less than " + b ); } else if ( a > b ) { alert( a + " is greater than " + b ); } else { alert( a + " must be equal to " + b ); } Use logical operators to add more expressions to evaluate in condition statements: var a = 7; if ( a > 5 && a < 10 ) { alert( a + " falls between 5 and 10" ); } else { alert( a + " does not fall between 5 and 10" ); }

JavaScript

Statements

breakconstcontinuedo whileforfor inif elselabelreturnswitchtry catchvarwhilewith