Skip to main content

The "Switch" Statement

JavaScript Fundamentals: The "Switch" Statement

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.

Technical Response: A switch statement can replace multiple checks, and it gives a more descriptive way to compare a value with multiple variants and is easier to read.

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. A default value gets used if the case 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]
}

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:
alert('Too small');
case 4:
alert('Exactly!');
case 5:
alert('Too big');
default:
alert("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, both switch and case allow arbitrary expressions.

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: We can group case variants into aggregated groups supplying them with a return value. A break statement should follow all case groups to ensure proper behavior and favorable outcomes.

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

let a = 3;

switch (a) {
case 4:
alert('Right!');
break;

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

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

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

View Answer:
Interview Response: Yes, the values must be of the same data type to match. The case value that does not, does not execute.

Code Example:

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

case '2':
alert('Two');
break;

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