switch (expression) {
case value1:
//Statements executed when the result of expression matches value1
[break;]
case value2:
//Statements executed when the result of expression matches value2
[break;]
...
case valueN:
//Statements executed when the result of expression matches valueN
[break;]
default:
//Statements executed when none of the values match the value of the expression
[break;]
}
expression
An expression whose result is matched against each case clause.
case valueNA case clause used to match against expression.
var foo = 0;
switch (foo) {
case -1:
console.log('negative 1');
break;
case 0:
console.log(0);
// NOTE: the forgotten break
case 1:
console.log(1);
break; // break
case 2:
console.log(2);
break;
}
Console output: 01
var Animal = 'Giraffe';
switch (Animal) {
case 'Cow':
case 'Giraffe':
case 'Dog':
case 'Pig':
console.log('This animal will go on Noah\'s Ark.');
break;
case 'Dinosaur':
default:
console.log('This animal will not.');
}
Read more: Stackoverflow: Switch statement multiple cases in JavaScript
@nbkeeprocki54 thanks, updated!
Sep 23, 7:33:05 AM JavaScript
Thanks @nbkeeprocki54, updated!
Sep 23, 7:27:32 AM JavaScript
There isn't the function call.
Sep 11, 6:28:44 AM JavaScript