Skip to content
AgileLogics
4 min read

if / else if / else

Prerequisites: Conditional statements

else if checks another condition when the first one is false; else catches everything else.

Syntax

if (condition1) {
    // ...
} else if (condition2) {
    // ...
} else {
    // ...
}

Example

const age = 15;

if (age >= 18) {
    console.log("Adult");
} else if (age >= 13) {
    console.log("Teenager");
} else {
    console.log("Child");
}

Expected output

Teenager

JavaScript checks each condition top to bottom and runs only the first block whose condition is true.

Practice

Write a program with a score variable that logs "Pass" if it's 50 or above, otherwise "Fail".

Show solution
const score = 42;

if (score >= 50) {
    console.log("Pass");
} else {
    console.log("Fail");
}