Skip to main content

The "Switch" Statement - JavaScript Interview

JavaScript Fundamentals: The "Switch" Statement




What is a switch statement in JavaScript?

View Answer:
Interview Response: A switch statement in JavaScript is a control flow statement that evaluates an expression and executes a specific block of code based on the matched case.


What are the advantages of employing a Switch statement?

View Answer:
Interview Response: A switch statement can replace multiple checks, and it is more descriptive and easier to read. Switch statements improve code readability, provide better performance, simplify complex conditionals, enhance maintainability, and support cleaner syntax.

Code Example: The SWITCH Statement

let x = 0;
switch (x) {
case 0:
text = 'Off';
break;
case 1:
text = 'On';
break;
default:
text = 'No value found';
}

How does the Switch statement work?

View Answer:
Interview Response: The switch statement works by checking the initial value against the case values. If the initial value equals one of the case values, it stops. An optional default value is used if the switch condition does not equal one of the case values.

Code Example: Syntax

let x = 'value2';

switch(x) {
case 'value1': // if (x === 'value1')
...
[break]

case 'value2': // if (x === 'value2')
...
[break]

default:
...
[break]
}

What happens if there is no match in a switch statement?

View Answer:
Interview Response: If no match is found in a switch statement, the code inside the default case executes, if provided. Otherwise, the switch statement exits without action.

Code Example:

Here is an example of a switch statement with and without a default case.

let fruit = "apple";

switch (fruit) {
case "banana":
console.log("I am a banana.");
break;
case "orange":
console.log("I am an orange.");
break;
default:
console.log("Unknown fruit."); // This line will execute
}

let vegetable = "carrot";

switch (vegetable) {
case "potato":
console.log("I am a potato.");
break;
case "tomato":
console.log("I am a tomato.");
break;
// No default case
}

// Output:
// Unknown fruit.

In the first switch statement, because there's no case for "apple", the default case is executed. In the second switch statement, because there's no case for "carrot" and no default case, the entire switch statement is skipped.


What is the purpose of the break statement in a switch statement?

View Answer:
Interview Response: The `break` statement in a JavaScript `switch` statement is used to prevent the code from running into the next `case` once a match is found and its code has been executed.

Do you have to use the break directive in the Switch statement?

View Answer:
Interview Response: No, but we should proceed with caution because the execution continues to the proceeding cases without any checks. We should use the break statement according to the specification.

Code Example: An example without break

let a = 2 + 2;

switch (a) {
case 3:
console.log('Too small');
case 4:
console.log('Exactly!');
case 5:
console.log('Too big');
default:
console.log("I don't know such values");
}

// Output:
// 'Exactly'
// 'Too Big'
// 'I don't know such values'

Can you use expressions in switch/case arguments?

View Answer:
Interview Response: Yes, it's possible to use expressions as arguments for switch and case statements, as long as they evaluate to a valid constant value or variable for comparison.

Code Example:

let a = '1';
let b = 0;

switch (+a) {
case b + 1:
console.log('this runs, because +a is 1, exactly equals b+1');
break;
default:
console.log("this doesn't run");
}
// Output: this runs, because +a is 1, exactly equals b+1

//////////////////////////////////////

let a = 10;
let b = 0;

switch (a * 10) {
case 100:
console.log('this runs, because +a is 1, exactly equals b+1');
break;
default:
console.log("this doesn't run");
}
// Output: this runs, because a * 10 = 100

Can you group switch cases as variant options?

View Answer:
Interview Response: Yes, you can group switch cases as variant options by stacking case labels without any code or break between them, allowing multiple cases to share a single block.

Code Example: For instance, suppose we want the identical code to run for cases 3 and 5.

let a = 3;

switch (a) {
case 4:
console.log('Right!');
break;

case 3: // (*) grouped two cases
case 5:
console.log('Wrong!');
console.log("Why don't you take a math class?");
break;

default:
console.log('The result is strange. Really.');
}

Does a switch statement have a strict equality check on value matching?

View Answer:
Interview Response: Switch statements use strict equality checks for value matching, meaning both value and type must match for a case to execute. No type coercion occurs during comparison.

Code Example:

let arg = prompt('Enter a value?');
switch (arg) {
case '0':
case '1':
console.log('One or zero');
break;

case '2':
console.log('Two');
break;

case 3:
console.log('Never executes!');
break;
default:
console.log('An unknown value');
}

Is the order of case statements important in a switch statement?

View Answer:
Interview Response: The order of case statements is important in a switch statement, especially when employing fall-through behavior. Cases are evaluated sequentially, so a matching case found earlier will prevent subsequent cases from being tested, affecting execution and performance.

Code Example:

let value = 'apple';

switch (value) {
case 'apple':
console.log('Apple');
break;
case 'apple':
console.log('Second Apple');
break;
default:
console.log('Default');
}

In this code, the output will be 'Apple'. Even though there's a second case for 'apple', it never gets executed because the first match triggers a break, stopping the switch.