if / else
Try it yourself →Runs different code based on a condition.
Syntax
if (condition) { ... } else { ... }Description
The condition is written inside parentheses, and blocks are written inside curly braces { }. Intermediate conditions are added with else if.
Example
const score = 75;
if (score >= 50) {
console.log("Passed");
} else {
console.log("Failed");
}Expected output
Passed
When to use?
Access control, flow branching.
Common mistakes
- ⚠️ Use === for equality (== can do type coercion and cause unexpected behavior).