What is a guard clause?
if (!user) return "Please log in"; at the top keeps the main logic un-nested and easy to read.
Conditional Statements · Exercise 1 of 4
Fifteen multiple-choice questions on JavaScript conditionals: if and else-if, truthy conditions, the dangling else, the ternary operator, switch matching and fall-through, default, switch (true), guard clauses and lookup tables. Pick an option to check your answer instantly.
What is a guard clause?
if (!user) return "Please log in"; at the top keeps the main logic un-nested and easy to read.
When is an object lookup like PRICES[size] ?? 0 a better choice than a switch?
A data mapping is shorter as data: easy to extend, reuse and test. Ranges and side effects are better as if/else.
What does if (x = 5) { … } do?
An assignment is an expression whose value is the assigned value. It is legal but almost always a bug – use ===.
user is undefined. What happens with if (user?.isAdmin) { … }?
user?.isAdmin evaluates to undefined when user is null or undefined, and undefined is falsy.
Why does case NaN: never match, even when the switch value is NaN?
NaN is not equal to itself under ===. Check Number.isNaN(value) with an if before the switch.
Timed online tests with instant scores and explanations.