Skip to content

if-else

The if statement executes a block of code if a condition is truthy. else if and else provide alternative branches.

if (condition) {
// runs if condition is truthy
} else if (otherCondition) {
// runs if first condition is falsy and otherCondition is truthy
} else {
// runs if all conditions are falsy
}
flowchart TD
Start["Start"]
Cond{"age >= 18?"}
Yes["You are an adult"]
No["You are a minor"]
Start --> Cond
Cond -->|Yes| Yes
Cond -->|No| No
const age = 20;
if (age >= 18) {
console.log('You can vote');
} else if (age >= 16) {
console.log('You can drive');
} else {
console.log('Too young');
}
if ('hello') { } // runs — truthy
if (0) { } // doesn't run — falsy
if (null) { } // doesn't run — falsy
if ([]) { } // runs — truthy (empty array)
if ({}) { } // runs — truthy (empty object)
  • Always use === for comparisons (not ==)
  • Prefer switch for many conditions on the same value
  • Use guard clauses to avoid deep nesting