JavaScript Nested If Condition

๐Ÿ“˜ What is Nested If?

A nested if condition is an if statement inside another if statement. It allows you to test multiple conditions sequentially.

๐Ÿ”น Syntax

if (condition1) {
    // code if condition1 is true
    if (condition2) {
        // code if condition2 is also true
    }
} else {
    // code if condition1 is false
}
            

๐Ÿ’ป Example

let age = 20;
let score = 85;

if (age >= 18) {
    if (score >= 80) {
        console.log("Adult with high score");
    } else {
        console.log("Adult with low score");
    }
} else {
    console.log("Not an adult");
}
            

๐Ÿงช Try It Yourself

Click the button to see nested if conditions in action: