JS Fundamentals - Switch Flashcards

1
Q

Rules are similar to java

A

-

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Expression

A

Any expression can be a switch/case argument

Both switch and case allow arbitrary expressions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Grouping of “case”

A

Several variants of case which share the same code can be grouped.

For example, if we want the same code to run for case 3 and case 5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Type matters

A
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' );
}

For 0, 1, the first alert runs.
For 2 the second alert runs.
But for 3, the result of the prompt is a string “3”, which is not strictly equal === to the number 3. So we’ve got a dead code in case 3! The default variant will execute.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly