The JavaScript switch
statement is used in decision making.
The switch
statement evaluates an expression and executes the corresponding body that matches the expression's result.
The syntax of the switch
statement is:
switch(variable/expression) {
case value1:
// body of case 1
break;
case value2:
// body of case 2
break;
case valueN:
// body of case N
break;
default:
// body of default
}
The switch
statement evaluates a variable/expression inside parentheses ()
.
value1
, its body is executed.value2
, its body is executed.default
body executes.Notes:
break
statement is optional. If the break statement is encountered, the switch statement ends.break
statement is not used, the cases after the matching case are also executed.default
clause is also optional.Flowchart of JavaScript switch statement
// program using switch statement
let a = 2;
switch (a) {
case 1:
a = 'one';
break;
case 2:
a = 'two';
break;
default:
a = 'not found';
break;
}
console.log(`The value is ${a}`);
Output
The value is two.
In the above program, an expression a = 2
is evaluated with a switch
statement.
case 1
which results in false
.switch
statement goes to the second case. Here, the expression's result matches with case 2
. So The value is two is displayed.break
statement terminates the block and control flow of the program jumps to outside of the switch
block.// program using switch statement
let a = 1;
switch (a) {
case "1":
a = 1;
break;
case 1:
a = 'one';
break;
case 2:
a = 'two';
break;
default:
a = 'not found';
break;
}
console.log(`The value is ${a}`);
Output
The value is one.
In the above program, an expression a = 1
is evaluated with a switch
statement.
case "1"
.switch
statement goes to the second case. Here, the expressions's result matches with case 1
. So The value is one is displayed.break
statement terminates the block and control flow of the program jumps to outside of the switch
block.Note: In JavaScript, the switch statement checks the cases strictly (should be of the same data type) with the expression's result. Notice in the above example, 1 does not match with "1".
Let's write a program to make a simple calculator with the switch
statement.
// program for a simple calculator
let result;
// take the operator input
const operator = prompt('Enter operator ( either +, -, * or / ): ');
// take the operand input
const number1 = parseFloat(prompt('Enter first number: '));
const number2 = parseFloat(prompt('Enter second number: '));
switch(operator) {
case '+':
result = number1 + number2;
console.log(`${number1} + ${number2} = ${result}`);
break;
case '-':
result = number1 - number2;
console.log(`${number1} - ${number2} = ${result}`);
break;
case '*':
result = number1 * number2;
console.log(`${number1} * ${number2} = ${result}`);
break;
case '/':
result = number1 / number2;
console.log(`${number1} / ${number2} = ${result}`);
break;
default:
console.log('Invalid operator');
break;
}
Output
Enter operator: +
Enter first number: 4
Enter second number: 5
4 + 5 = 9
In above program, the user is asked to enter either +, -, * or /, and two operands. Then, the switch
statement executes cases based on the user input.
In a JavaScript switch statement, cases can be grouped to share the same code.
// multiple case switch program
let fruit = 'apple';
switch(fruit) {
case 'apple':
case 'mango':
case 'pineapple':
console.log(`${fruit} is a fruit.`);
break;
default:
console.log(`${fruit} is not a fruit.`);
break;
}
Output
apple is a fruit.
In the above program, multiple cases are grouped. All the grouped cases share the same code.
If the value of the fruit variable had value mango
or pineapple
, the output would have been the same.