Skip to main content

Conditional statements in javascript

Very often when you write code, you want to perform different actions for different decisions.

You can use conditional statements in your code to do this.

In JavaScript we have the following conditional statements:

  • Use if to specify a block of code to be executed, if a specified condition is true
  • Use else to specify a block of code to be executed, if the same condition is false
  • Use else if to specify a new condition to test, if the first condition is false
  • Use switch to specify many alternative blocks of code to be executed

The if Statement

Use the if statement to specify a block of JavaScript code to be executed if a condition is true.

Syntax

if (condition) {
// block of code to be executed if the condition is true
}

Example Make a "Eligible to vote" message if the age is greater than 18:

if (age > 18) {
message = "Eligible to vote";
}

The result of greeting will be:

Eligible to vote

The else Statement

Use the else statement to specify a block of code to be executed if the condition is false.

if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}

Example If the age is less than 18, create a "Not Eligible to vote" message, otherwise "Eligible to vote":

if (age > 18) {
message = "Eligible to vote";
} else {
message = "Not Eligible to vote";
}

The result of message will be:

Eligible to vote

The else if Statement

Use the else if statement to specify a new condition if the first condition is false.

Syntax

if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and condition2 is true
} else {
// block of code to be executed if the condition1 is false and condition2 is false
}

Example

If mark is greater than 90 then print S grade, if not, but mark is greater than 80 then print A grade, otherwise print B grade.

if (mark > 90) {
message = "S grade";
} else if (mark > 80) {
message = "A grade";
} else {
message = "B grade";
}

The result of message will be:

S grade

JavaScript Switch Statement

The switch statement is used to perform different actions based on different conditions.

Use the switch statement to select one of many code blocks to be executed.

Syntax
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}

Example

The getDay() method returns the weekday as a number between 0 and 6.

(Sunday=0, Monday=1, Tuesday=2 ..)

This example uses the weekday number to calculate the weekday name:

const today = new Date().getDay();

switch (today) {
case 0:
day = "Sunday";
break;
case 1:
day = "Monday";
break;
case 2:
day = "Tuesday";
break;
case 3:
day = "Wednesday";
break;
case 4:
day = "Thursday";
break;
case 5:
day = "Friday";
break;
case 6:
day = "Saturday";
}

The above programs result will be today's name example: Wednesday