JavaScript - If...Else
Overview
Estimated time: 25–35 minutes
Control flow with if/else. Learn chained conditions and the ternary operator.
Learning Objectives
- Write conditional logic using if/else if/else.
- Use the ternary operator for simple expressions.
Prerequisites
Examples
const x = 5;
if (x > 10) {
// ...
} else if (x > 0) {
// ...
} else {
// ...
}
const y = (x > 0 ? 1 : -1);
Common Pitfalls
- Using assignment (=) instead of comparison (===) in conditions.
Exercises
- Write a function that classifies an integer as negative, zero, or positive.