Which comparison does a switch statement use to match a case?
switch uses ===, so case 2 does not match the string "2", and no type conversion happens.
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.
Which comparison does a switch statement use to match a case?
switch uses ===, so case 2 does not match the string "2", and no type conversion happens.
What happens when a matching case has no break (or return)?
This is fall-through. It lets several cases share code (case "Sat": case "Sun": …) and causes bugs when a break is forgotten.
Which statement about default in a switch is correct?
default is chosen after all cases fail to match, wherever it is written. It is optional, and only one default is allowed.
In an if / else-if / else chain where several conditions are true, which branches run?
Once a branch runs, the rest of the chain is skipped – which is why the order of the tests matters.
Which of these conditions makes the if-block run?
Any non-empty string is truthy, even "false". 0, "" and null are falsy.
In if (a) if (b) x(); else y(); written without braces, which if does the else belong to?
This is the dangling else rule. y() runs when a is true and b is false. Braces make the intent explicit.
What is the purpose of switch (true) { case score >= 90: … }?
Each case expression evaluates to true or false and is compared with true using ===, so the first true condition wins.
What can the ternary operator do that an if statement cannot?
const fee = isMember ? 0 : 99; works because the ternary is an expression. if is a statement and cannot appear on the right of =.
A variable is declared with let inside an if block. Where can it be used?
let and const are block-scoped. Declare the variable before the if when you need it afterwards.
A switch has case f():, case g(): and case h():, and the value matches what g() returns. Which functions are called?
Case expressions can be any expression and are evaluated lazily, top to bottom, stopping at the first match.
Timed online tests with instant scores and explanations.