Bird
Raised Fist0
Javascriptprogramming~20 mins

Switch vs if comparison in Javascript - Practice Questions

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Switch Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of switch with fall-through
What is the output of this JavaScript code?
const value = 2;
switch(value) {
  case 1:
    console.log('One');
  case 2:
    console.log('Two');
  case 3:
    console.log('Three');
    break;
  default:
    console.log('Default');
}
Javascript
const value = 2;
switch(value) {
  case 1:
    console.log('One');
  case 2:
    console.log('Two');
  case 3:
    console.log('Three');
    break;
  default:
    console.log('Default');
}
ATwo\nThree\nDefault
BTwo\nThree
COne\nTwo\nThree
DDefault
Attempts:
2 left
💡 Hint
Remember that without break, switch cases fall through to the next.
❓ Predict Output
intermediate
2:00remaining
Equivalent if-else output
What is the output of this if-else code?
const value = 2;
if (value === 1) {
  console.log('One');
} else if (value === 2) {
  console.log('Two');
} else if (value === 3) {
  console.log('Three');
} else {
  console.log('Default');
}
Javascript
const value = 2;
if (value === 1) {
  console.log('One');
} else if (value === 2) {
  console.log('Two');
} else if (value === 3) {
  console.log('Three');
} else {
  console.log('Default');
}
ADefault
BTwo\nThree
COne
DTwo
Attempts:
2 left
💡 Hint
Only one block runs in if-else chains.
❓ Predict Output
advanced
2:00remaining
Switch with multiple cases combined
What will this code print?
const fruit = 'apple';
switch(fruit) {
  case 'banana':
  case 'apple':
    console.log('Yellow or Red fruit');
    break;
  case 'orange':
    console.log('Orange fruit');
    break;
  default:
    console.log('Unknown fruit');
}
Javascript
const fruit = 'apple';
switch(fruit) {
  case 'banana':
  case 'apple':
    console.log('Yellow or Red fruit');
    break;
  case 'orange':
    console.log('Orange fruit');
    break;
  default:
    console.log('Unknown fruit');
}
AYellow or Red fruit
BOrange fruit
CUnknown fruit
Dbanana
Attempts:
2 left
💡 Hint
Cases stacked without break share the same code block.
❓ Predict Output
advanced
2:00remaining
If vs switch with type coercion
What is the output of this code?
const val = '2';
switch(val) {
  case 2:
    console.log('Number 2');
    break;
  case '2':
    console.log('String 2');
    break;
  default:
    console.log('Default');
}
Javascript
const val = '2';
switch(val) {
  case 2:
    console.log('Number 2');
    break;
  case '2':
    console.log('String 2');
    break;
  default:
    console.log('Default');
}
AString 2
BDefault
CNumber 2
DTypeError
Attempts:
2 left
💡 Hint
Switch uses strict equality (===) for matching.
🧠 Conceptual
expert
2:00remaining
Why prefer switch over if in some cases?
Which of the following is the best reason to use a switch statement instead of multiple if-else statements in JavaScript?
ASwitch statements automatically break after each case without needing a break statement.
BSwitch statements can evaluate complex expressions better than if-else.
CSwitch statements improve readability when checking one variable against many constant values.
DSwitch statements allow asynchronous code execution inside cases.
Attempts:
2 left
💡 Hint
Think about how code looks when checking many values of one variable.

Practice

(1/5)
1.

Which statement best describes when to use switch instead of if in JavaScript?

easy
A. Use switch for complex conditions involving ranges or multiple variables.
B. Use switch only for boolean true/false checks.
C. Use switch when checking one value against many fixed options.
D. Use switch to replace all if statements regardless of condition type.

Solution

  1. Step 1: Understand the purpose of switch

    switch is designed to compare one value against many possible fixed cases.
  2. Step 2: Compare with if usage

    if is better for complex or varied conditions, like ranges or multiple variables.
  3. Final Answer:

    Use switch when checking one value against many fixed options. -> Option C
  4. Quick Check:

    switch = fixed value checks [OK]
Hint: Use switch for one value, many options; if for complex checks [OK]
Common Mistakes:
  • Using switch for complex range conditions
  • Thinking switch works with multiple variables
  • Believing switch can replace all if statements
2.

Which of the following is the correct syntax to start a switch statement in JavaScript?

?
easy
A. switch (variable) {
B. switch variable {
C. switch { variable }
D. switch: (variable) {

Solution

  1. Step 1: Recall switch syntax

    The correct syntax requires parentheses around the variable: switch (variable) {.
  2. Step 2: Identify incorrect options

    Options A, B, and D have syntax errors like missing parentheses or wrong punctuation.
  3. Final Answer:

    switch (variable) { -> Option A
  4. Quick Check:

    Parentheses needed after switch [OK]
Hint: Remember: switch needs parentheses around the expression [OK]
Common Mistakes:
  • Omitting parentheses after switch
  • Using curly braces instead of parentheses
  • Adding colon after switch keyword
3.

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');
}
medium
A. Yellow
B. Red
C. Unknown
D. No output

Solution

  1. Step 1: Check the value of fruit

    The variable fruit is set to 'apple'.
  2. Step 2: Match fruit in the switch cases

    The case 'apple' matches, so it prints 'Red' and then breaks.
  3. Final Answer:

    Red -> Option B
  4. Quick Check:

    fruit='apple' matches case 'apple' [OK]
Hint: Match value exactly in switch cases to find output [OK]
Common Mistakes:
  • Forgetting break causes fall-through
  • Assuming default runs even if case matches
  • Mixing up case values
4.

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');
}
medium
A. Add parentheses around day in switch: switch (day) {
B. Replace case 3: with case '3':
C. Remove all break; statements
D. Change default to else

Solution

  1. Step 1: Identify syntax error in switch statement

    The switch statement is missing parentheses around the variable: it should be switch (day) {.
  2. Step 2: Evaluate other options

    Changing case 3 to string is unnecessary; breaks are needed; default cannot be else.
  3. Final Answer:

    Add parentheses around day in switch: switch (day) { -> Option A
  4. Quick Check:

    switch needs parentheses [OK]
Hint: Always put parentheses after switch keyword [OK]
Common Mistakes:
  • Omitting parentheses after switch
  • Confusing case value types
  • Removing break causing fall-through
5.

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?

hard
A. Use a switch statement with cases for each range.
B. Use a single if statement with all conditions combined.
C. Use a switch with boolean expressions in cases.
D. Use multiple if and else if statements to check ranges.

Solution

  1. Step 1: Analyze the conditions

    The conditions involve ranges, not fixed values, so switch is not suitable.
  2. Step 2: Choose the best control structure

    Multiple if and else if statements can check ranges easily and clearly.
  3. Final Answer:

    Use multiple if and else if statements to check ranges. -> Option D
  4. Quick Check:

    Ranges need if/else if, not switch [OK]
Hint: Use if/else for ranges; switch only for exact matches [OK]
Common Mistakes:
  • Trying to use switch for range conditions
  • Using switch with boolean expressions (not allowed)
  • Combining all conditions in one if without else if
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard