JS-046
JavaScript diagram about Nested if: a visual example of An if statement inside another if
Beginner4 min~4 min read#nested-if#if#conditions

Nested if

An if statement inside another if

Goal

Learn to use one if statement inside another for multi-level checks.

Explanation

  • Nested if means an if statement placed inside another if.
  • The outer condition is checked first.
  • Only when it is true does the inner if run.
  • This lets you build more complex conditions step by step.
  • Nested if statements are often used to check access, roles, and user permissions.

Analogy

To enter a VIP area, you must first pass through the main entrance. Only then is your VIP status checked.

Code

if (loggedIn) {
  if (isVIP) {
    console.log("Access");
  }
}

Result: Access

Common mistake

Beginners may think all if statements are checked at the same time. In fact, the inner if runs only after the outer one passes.

Remember

  • The inner if is checked only after the outer one passes.
  • Nested if means an if statement inside another if.
  • It lets you create checks that run in sequence.

Quick Quiz

When does the inner if run?

Resources