A switch statement can replace multiple if checks.
The switch has one or more case blocks and an optional default.
switch(x) {
case 'value1': // if ( x === 'value1')
...
[break]
case 'value2': // if ( x === 'value2')
...
[break]
default:
...
[break]
}
→ If there is no break then the execution continues with the next case without any checks.
Several variants of case which share the same code can be grouped.
equality check는 항상 strict이다. value는 match하기 위해 항상 같은 타입이어야 한다.
// 1
let a = prompt("Please enter a number");
console.log(a);
typeof(a);
console.log(`typeof: ${typeof(a)}`)
// 2
let a = +prompt("Please enter a number");
console.log(a);
typeof(a);
console.log(`typeof: ${typeof(a)}`)
→ before prompt add '+' that converts value from string to number