We use switch and if to make decisions in code. They help the program choose what to do based on different conditions.
Switch vs if comparison in Javascript
switch (expression) { case value1: // code to run if expression === value1 break; case value2: // code to run if expression === value2 break; default: // code to run if no case matches } // if statement if (condition1) { // code if condition1 is true } else if (condition2) { // code if condition2 is true } else { // code if none of the above conditions are true }
The switch compares one expression to many values.
The if can check any condition, not just equality.
switch checks the value of day and prints a message.switch (day) { case 'Monday': console.log('Start of the week'); break; case 'Friday': console.log('Almost weekend'); break; default: console.log('Just another day'); }
if checks ranges of score and prints the grade.if (score >= 90) { console.log('Grade A'); } else if (score >= 80) { console.log('Grade B'); } else { console.log('Grade C or below'); }
switch.const color = 'red'; switch (color) { case 'red': case 'pink': console.log('Warm color'); break; case 'blue': console.log('Cool color'); break; default: console.log('Unknown color'); }
This program shows how switch and if can do the same thing: check the fruit and print its color.
const fruit = 'apple'; switch (fruit) { case 'banana': console.log('Yellow fruit'); break; case 'apple': console.log('Red or green fruit'); break; case 'orange': console.log('Orange fruit'); break; default: console.log('Unknown fruit'); } if (fruit === 'banana') { console.log('Yellow fruit'); } else if (fruit === 'apple') { console.log('Red or green fruit'); } else if (fruit === 'orange') { console.log('Orange fruit'); } else { console.log('Unknown fruit'); }
Remember to use break in switch to stop running other cases.
if is more flexible for complex conditions.
switch is easier to read when checking one variable against many values.
switch is good for checking one value against many options.
if is better for complex or different conditions.
Both help your program decide what to do next.
Practice
Which statement best describes when to use switch instead of if in JavaScript?
Solution
Step 1: Understand the purpose of
switchswitchis designed to compare one value against many possible fixed cases.Step 2: Compare with
ifusageifis better for complex or varied conditions, like ranges or multiple variables.Final Answer:
Useswitchwhen checking one value against many fixed options. -> Option CQuick Check:
switch= fixed value checks [OK]
- Using switch for complex range conditions
- Thinking switch works with multiple variables
- Believing switch can replace all if statements
Which of the following is the correct syntax to start a switch statement in JavaScript?
?
Solution
Step 1: Recall
The correct syntax requires parentheses around the variable:switchsyntaxswitch (variable) {.Step 2: Identify incorrect options
Options A, B, and D have syntax errors like missing parentheses or wrong punctuation.Final Answer:
switch (variable) { -> Option AQuick Check:
Parentheses needed after switch [OK]
- Omitting parentheses after switch
- Using curly braces instead of parentheses
- Adding colon after switch keyword
What will be the output of the following code?
const fruit = 'apple';
switch (fruit) {
case 'banana':
console.log('Yellow');
break;
case 'apple':
console.log('Red');
break;
default:
console.log('Unknown');
}Solution
Step 1: Check the value of
The variablefruitfruitis set to 'apple'.Step 2: Match
Thefruitin the switch casescase 'apple'matches, so it prints 'Red' and then breaks.Final Answer:
Red -> Option BQuick Check:
fruit='apple' matches case 'apple' [OK]
- Forgetting break causes fall-through
- Assuming default runs even if case matches
- Mixing up case values
Find the error in this switch statement and choose the fix:
const day = 3;
switch day {
case 1:
console.log('Monday');
break;
case 3:
console.log('Wednesday');
break;
default:
console.log('Other day');
}Solution
Step 1: Identify syntax error in switch statement
The switch statement is missing parentheses around the variable: it should beswitch (day) {.Step 2: Evaluate other options
Changing case 3 to string is unnecessary; breaks are needed; default cannot be else.Final Answer:
Add parentheses arounddayin switch:switch (day) {-> Option AQuick Check:
switch needs parentheses [OK]
- Omitting parentheses after switch
- Confusing case value types
- Removing break causing fall-through
You want to write a program that prints a message based on a score:
- If score is exactly 100, print 'Perfect!'
- If score is between 90 and 99, print 'Great!'
- If score is between 60 and 89, print 'Good'
- Otherwise, print 'Try again'
Which approach is best?
Solution
Step 1: Analyze the conditions
The conditions involve ranges, not fixed values, soswitchis not suitable.Step 2: Choose the best control structure
Multipleifandelse ifstatements can check ranges easily and clearly.Final Answer:
Use multipleifandelse ifstatements to check ranges. -> Option DQuick Check:
Ranges need if/else if, not switch [OK]
- Trying to use switch for range conditions
- Using switch with boolean expressions (not allowed)
- Combining all conditions in one if without else if
Ready to prove it?
Pass the test and this pattern moves unknown
