0
0
Javascriptprogramming~20 mins

Switch vs if comparison in Javascript - Practice Questions

Choose your learning style9 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.