Conditional statements
Conditional statements in JavaScript are the ways to run different branches of code depending on the value of an expression: if / else if / else, the ternary operator ? :, switch, and the logical short-circuits &&, || and ??. They all perform the same branching, but differ in readability and in whether they produce a value.
Theory
TL;DR
if / else if / elseis the basic construct; the condition is coerced to a boolean (truthy/falsy).- The ternary
condition ? a : bis an expression, so its result can be assigned. switchcompares one expression against a set ofcaselabels with strict equality===;breakprevents fall-through anddefaultis the equivalent ofelse.&&evaluates the right-hand side only when the left one is truthy.||returns the first truthy value, which is handy for defaults.??substitutes a value only fornullandundefined, unlike||.
Quick example
const age = 20;
if (age >= 18) {
console.log('Adult');
} else if (age >= 14) {
console.log('Teenager');
} else {
console.log('Child');
}ifchecks the condition.else ifadds further checks.elseruns when none of the previous ones matched.
The condition inside
ifis coerced to a boolean value (truthy/falsy).
The ternary operator ? :
A short form for simple conditions which, unlike if, is an expression and returns a value.
const age = 18;
const status = age >= 18 ? 'Adult' : 'Minor';
console.log(status); // "Adult"The syntax is condition ? value_if_true : value_if_false.
Several ternaries can be nested, but watch the readability:
const age = 25;
const group =
age < 13 ? 'child' :
age < 20 ? 'teenager' :
'adult';The switch statement
Convenient when there are many options for one and the same expression.
const day = 3;
switch (day) {
case 1:
console.log('Monday');
break;
case 2:
console.log('Tuesday');
break;
case 3:
console.log('Wednesday');
break;
default:
console.log('Unknown day');
}breakprevents "falling through" into the nextcase.defaultis the equivalent ofelse.
Branches can also be grouped:
switch (day) {
case 6:
case 7:
console.log('Weekend');
break;
default:
console.log('Working day');
}One important detail: switch compares values with strict equality ===, so switch ('3') will not land in case 3.
The logical operators &&, ||, ??
Sometimes an if can be replaced with a logical short-circuit.
&&, "and" (runs the second part when the first is truthy)
const isAdmin = true;
isAdmin && console.log('Welcome, admin!');If isAdmin === true, the console.log runs.
||, "or" (returns the first truthy value)
const userName = '' || 'Guest';
console.log(userName); // "Guest"Handy for default values.
??, nullish coalescing (only for null and undefined)
const userInput = null;
const value = userInput ?? 'Default';
console.log(value); // "Default"The difference from ||: it does not fire on 0, '' or false.
Comparing the constructs
| Construct | Is it an expression? | When it fits |
|---|---|---|
if / else if / else | no | complex logic with a branch body |
? : | yes | a short choice between two values |
switch | no | many options of one expression, compared with === |
&&, ||, ?? | yes | a short check and a default value |
Common mistakes
- Mixing up
=and===in a condition.if (x = 5)assigns a value and is always truthy. - Forgetting
breakin aswitch. Execution "falls through" into the next branches and severalcaselabels run in a row. - Expecting loose equality in a
switch. The comparison is strict, so'3'and3are different branches. - Using
||for defaults where0or''are valid values.count || 10turns a genuine0into10;??is what you want there. - Nesting ternaries three or more levels deep. It technically works, but such code is hard to read; prefer an
ifor a lookup object. - Mixing
??with||or&&without parentheses. That is aSyntaxError, explicit parentheses are required.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.