Skip to content

JS Else If Statement

Syntax

The else if statement in JavaScript is used to specify a new condition to test if the previous if condition evaluates to false. It can be used multiple times and can be used with or without an else statement.

// Single else if
if (condition1) {
// Code to execute if condition1 is true
} else if (condition2) {
// Code to execute if condition2 is true
}
// Multiple else if
if (condition1) {
// Code to execute if condition1 is true
} else if (condition2) {
// Code to execute if condition2 is true
} else if (condition3) {
// Code to execute if condition3 is true
}
// With else
if (condition1) {
// Code to execute if condition1 is true
} else if (condition2) {
// Code to execute if condition2 is true
} else if (condition3) {
// Code to execute if condition3 is true
} else {
// Code to execute if all conditions are false
}

Example

let age = 19;
if (age < 13) {
console.log("You are a child.");
} else if (age < 20) {
console.log("You are a teenager.");
} else {
console.log("You are an adult.");
}
// Output: You are a teenager.