< Javascript Tutorial

Javascript if and else

← Previous Next →


Sometimes javascript requires the ability to make decision between different possibilities.
JavaScript supports conditional statements which are used to perform different actions based on different conditions.
Here we will explain the if..else statement.

The general syntax for if statements is:

if (condition) {
     action1
} 
else {
     action2
};

JavaScript supports the following forms of if..else statement −

if statement
if...else statement
if...else if... statement.

Syntax

if (expression){
      Statement(s) to be executed if expression is true
} else {
      Statement(s) to be executed if expression is false
}

Example:

if (hour < 18) {
       greeting = "Good day";
} else {
       greeting = "Good evening";
}
The result of greeting will be:

Good evening

← Previous Next →