Recall & Review
beginner
What is a switch statement used for in JavaScript?
A switch statement is used to perform different actions based on different conditions. It checks a value against multiple cases and runs the matching block of code.
Click to reveal answer
beginner
How do you write a basic switch statement syntax?
Use
switch(expression) { case value1: // code break; case value2: // code break; default: // code } to compare the expression with values and run matching code.Click to reveal answer
intermediate
What happens if you forget to add
break in a switch case?Without
break, the code will continue running the next cases even if they don't match. This is called "fall-through" behavior.Click to reveal answer
beginner
What is the purpose of the
default case in a switch statement?The
default case runs if none of the other cases match the expression. It acts like a fallback or else condition.Click to reveal answer
intermediate
Can a switch statement compare different data types?
Yes, but the comparison uses strict equality (===), so the type and value must match exactly for a case to run.
Click to reveal answer
What keyword stops the execution of more cases in a switch statement?
✗ Incorrect
The
break keyword stops the switch from running the next cases.What happens if no case matches and there is no default case?
✗ Incorrect
If no case matches and no default is provided, the switch does nothing and continues.
Which comparison does switch use between expression and case values?
✗ Incorrect
Switch uses strict equality (===), so type and value must match.
Can multiple cases share the same code block in a switch?
✗ Incorrect
You can stack cases without break to share the same code block.
What is the role of the default case?
✗ Incorrect
The default case runs when no other case matches the expression.
Explain how a switch statement works and why you need the break keyword.
Think about how you choose actions based on different options.
You got /4 concepts.
Describe what happens if you omit the default case in a switch statement.
Consider what happens when none of the options fit.
You got /3 concepts.