Conditional statements
Conditional statements let a program choose between different paths depending on whether something is true. Checking if a user is logged in, showing a discount only above a certain cart total, or handling a form differently for each option in a dropdown all rely on conditionals. JavaScript provides if, else if, else and switch for this, plus the ternary operator for short expressions.
if, else if and else
An if statement runs its block only when the condition in parentheses is truthy. An optional else block runs otherwise. To test several conditions in turn, chain them with else if. JavaScript checks them from top to bottom and runs only the first block whose condition is true, skipping the rest.
function deliveryCharge(cartTotal, isMember) {
if (cartTotal <= 0) {
return null; // nothing to deliver
} else if (isMember || cartTotal >= 999) {
return 0; // free delivery
} else if (cartTotal >= 499) {
return 40;
} else {
return 70;
}
}
console.log(deliveryCharge(1200, false)); // 0
console.log(deliveryCharge(600, false)); // 40
Order matters here. If the >= 499 check came first, a cart of 1200 would match it and never reach the free delivery branch.
Truthy and falsy values
A condition does not have to be a boolean. JavaScript converts it, and exactly these values count as false: false, 0, -0, 0n, "" (empty string), null, undefined and NaN. Everything else is truthy, including "0", "false", empty arrays and empty objects. This is convenient for checks like if (username), but it means an input value of 0 is treated as missing.
switch for many fixed values
When you compare one value against a list of specific options, switch can read more clearly than a long chain. It uses strict equality, so "1" does not match case 1.
function statusLabel(code) {
switch (code) {
case 'P':
return 'Pending';
case 'S':
case 'D':
return 'On its way'; // two cases share one result
case 'X':
return 'Cancelled';
default:
return 'Unknown';
}
}
In a switch that does not return, each case needs a break. Without it, execution "falls through" into the next case and runs that code too. Sometimes that is intended, as with 'S' and 'D' above, but forgetting break is a common source of strange output.
For simple either-or values, the ternary operator is a compact alternative: const label = stock > 0 ? 'In stock' : 'Sold out';. It is an expression, so it can sit inside template strings or JSX. Keep ternaries short; nesting them several levels deep is harder to read than an if chain.
Classic mistakes
Using a single = inside a condition assigns instead of comparing. if (role = 'admin') sets role and is always true. Use ===.
Loose equality == converts types before comparing, so 0 == '' and null == undefined are both true. Stick with === unless you have a specific reason.
Deeply nested if blocks also become hard to follow. Returning early for invalid cases, as the first branch of deliveryCharge does, keeps the main logic flat.
What to keep
- Conditions are checked top to bottom; put the most specific first.
- Know the eight falsy values.
- Always compare with
===. - Add
breakorreturnto everyswitchcase unless fall-through is intended.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.