Open In App

JavaScript if else else if

Last Updated : 12 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The JavaScript if, else, and else if statements are part of the conditional statements available in JavaScript. These conditional statements are used to execute a particular block of code based on the passed condition. It will execute the code statements only if the condition turns to true.

We have the following conditional statements available in JavaScript:

if statement

The if statement is defined using the if keyword and is always operated with an expression passed inside the round brackets just after its definition.

Syntax:

if (conditional_expression){
// Code statements to be executed if
// the passed expression evaluates to true
}

Example: The below code implements the if statement practically with an expression in JavaScript.

Javascript




const num1 = 45;
const num2 = 5;
 
if(num1 < 50){
    console.log(`${num1} is less than 50`);
}
 
if(num1%num2 === 0){
    console.log(`${num2} completely divides ${num1}`);
}


Output

45 is less than 50
5 completely divides 45


if else statement

The if else statement is an extended version of the if condition. It allows you to write code for both the cases i.e. either the expression returns true or false, you can handle both of them. The if statement will run for the true value while else for the false value.

Syntax:

if (conditional_expression){
// Code statements for true expression evaluation
}
else{
// Code statements for false expression evaluation
}

Example: The below code will explain the use of the if else statement in JavaScript.

Javascript




const num1 = 45;
const num2 = 8;
 
if(num1 < 50){
    console.log(`${num1} is less than 50`);
}
else{
    console.log(`${num1} is greater than 50`);
}
 
if(num1%num2 === 0){
    console.log(`${num2} completely divides ${num1}`);
}
else{
    console.log(`${num1} is not divisible by ${num2}`);
}


Output

45 is less than 50
45 is not divisible by 8


else if statement

The else if statement is used in the cases where you need to execute multiple code blocks based on multiple conditional expression. It starts with the if statement and ends with the else statement, it is written in between them.

Syntax:

if (conditional_expression1){
// Code to execute if expression1 turns to true
}
else if (conditional_expression2){
// Code to execute if expression2 turns to true
}
else{
// Code to execute if none of the expressions turns to true
}

Example: The below code will explain the practical use of the else if statement in JavaScript.

Javascript




const num1 = 45;
const num2 = 8;
 
if(num1 < 50){
    console.log(`${num1} is less than 50`);
}
else if(num1 === 50){
    console.log(`${num1} is equal to 50`);
}
else{
    console.log(`${num1} is greater than 50`);
}
 
if(num1+num2 === 50){
    console.log
    (`The sum of numbers ${num1} and ${num2} is equal to 50.`);
}
else if(num1+num2 > 50){
    console.log
    (`The sum of numbers ${num1} and ${num2} is greater than 50.`);
}
else{
    console.log
    (`The sum of numbers ${num1} and ${num2} is less than 50.`);
}
 
if(num1-num2 > 50){
    console.log
    (`The difference of numbers ${num1} and ${num2} is greater than 50.`);
}
else if(num1-num2 === 50){
    console.log
    (`The difference of numbers ${num1} and ${num2} is equal to 50.`);
}
else{
    console.log
    (`The difference of numbers ${num1} and ${num2} is less than 50.`);
}


Output

45 is less than 50
The sum of numbers 45 and 8 is greater than 50.
The difference of numbers 45 and 8 is less than 50.




Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads