Conditional statements allow us to represent such decision making in JavaScript, from the choice that must be made (for example, "one cookie or two"), to the resulting outcome of those choices (perhaps the outcome of "ate one cookie" might be "still felt hungry", and the outcome of "ate two cookies" might be "felt full, but mom scolded me for eating all the cookies".)
The switch
statement evaluates an expression, matching the expression's value against a series of case
clauses, and executes statements after the first case
clause with a matching value, until a break
statement is encountered. The default
clause of a switch
statement will be jumped to if no case
matches the expression's value.
In the following example, if expr
evaluates to Bananas
, the program matches the value with case case 'Bananas'
and executes the associated statement. When break
is encountered, the program breaks out of switch
and executes the statement following switch
. If break
were omitted, the statement for the case 'Cherries'
would also be executed.
switch (expr) {
case 'Oranges':
console.log('Oranges are $0.59 a pound.');
break;
case 'Apples':
console.log('Apples are $0.32 a pound.');
break;
case 'Bananas':
console.log('Bananas are $0.48 a pound.');
break;
case 'Cherries':
console.log('Cherries are $3.00 a pound.');
break;
case 'Mangoes':
case 'Papayas':
console.log('Mangoes and papayas are $2.79 a pound.');
break;
default:
console.log(`Sorry, we are out of ${expr}.`);
}
console.log("Is there anything else you'd like?");
If no match is found, execution will start from the default
clause, and execute all statements after that.
const foo = 5;
switch (foo) {
case 2:
console.log(2);
break; // it encounters this break so will not continue into 'default:'
default:
console.log('default')
// fall-through
case 1:
console.log('1');
}
It also works when you put default
before all other case
clauses.
This method takes advantage of the fact that if there is no break
below a case
clause, execution will continue to the next case
clause regardless if that case
meets the criteria.
The following is an example of a single operation sequential case
statement, where four different values perform exactly the same.
const Animal = 'Giraffe';
switch (Animal) {
case 'Cow':
case 'Giraffe':
case 'Dog':
case 'Pig':
console.log('This animal is not extinct.');
break;
case 'Dinosaur':
default:
console.log('This animal is extinct.');
}
The following is an example of a multiple-operation sequential case
clause, where, depending on the provided integer, you can receive different output. This shows you that it will traverse in the order that you put the case
clauses, and it does not have to be numerically sequential. In JavaScript, you can even mix in definitions of strings into these case
statements as well.
const foo = 1;
let output = 'Output: ';
switch (foo) {
case 0:
output += 'So ';
case 1:
output += 'What ';
output += 'Is ';
case 2:
output += 'Your ';
case 3:
output += 'Name';
case 4:
output += '?';
console.log(output);
break;
case 5:
output += '!';
console.log(output);
break;
default:
console.log('Please pick a number from 0 to 5!');
}
The output from this example:
Value | Log text |
---|---|
foo is NaN or not 1 , 2 , 3 , 4 , 5 , or 0 |
Please pick a number from 0 to 5! |
0 |
Output: So What Is Your Name? |
1 |
Output: What Is Your Name? |
2 |
Output: Your Name? |
3 |
Output: Name? |
4 |
Output: ? |
5 |
Output: ! |