0
0
Javascriptprogramming~5 mins

Switch statement in Javascript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
Astop
Bbreak
Cexit
Dreturn
What happens if no case matches and there is no default case?
ANothing happens, switch ends
BThe switch runs the first case
CAn error is thrown
DThe program crashes
Which comparison does switch use between expression and case values?
AStrict equality (===)
BLess than (<)
CGreater than (>)
DLoose equality (==)
Can multiple cases share the same code block in a switch?
AOnly if they have the same value
BNo, each case must have unique code
CYes, by stacking cases without break
DOnly inside the default case
What is the role of the default case?
ATo stop the switch statement
BTo declare variables
CTo repeat the first case
DTo run code if no other case matches
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.