JavaScript » Statements » if...else

Syntax:
if (condition) {statements1} [else {statements2}]

The if...else statement executes one set of statements if a specified condition is true, and another if it is false.

NOTE:
 
You shouldn't use simple assignment statements such as if(a = b) in a conditional statement. To compare two variables, remember to use the == operator.

Examples

Code:
if(calcaverage(x,y,z) < 10)
   document.write("The average is less than 10.");
else
   document.write("The average is 10 or more.");
Explanation:

This example tests the result of a function and displays one of two messages depending on whether the result is less than 10 or not: