Switch Statement in JavaScript

Switch Statement in JavaScript

In this article, we will learn about Switch Statement in JavaScript with the help of suitable examples.

In JavaScript, the switch statement is used to make decisions.

The switch statement evaluates an expression and executes the body that corresponds to the result of the expression.

The switch statement has the following syntax:

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 is used to evaluate a variable or expression that is enclosed in parentheses ().

  • If the expression’s result equals value1, the expression’s body is executed.
  • If the expression’s result equals value2, the expression’s body is executed.
  • This procedure continues. If no matching case is found, the default body is used.

Notes:

  • The break statement is not required. The switch statement is terminated if the break statement is encountered.
  • The cases after the matching case are also executed if the break statement is not used.
  • It’s also possible to leave out the default clause.

Example 1: Simple 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.

A switch statement is used in the above program to evaluate the expression a = 2.

  • The result of the expression is evaluated using case 1, which returns false.
  • The switch statement then moves on to the next case. The result of the expression is the same as in case 2. As a result, the value is two is displayed.
  • The break statement ends the block, and the program’s control flow jumps to the outside of the switch block.

Example 2: Type Checking in 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.

A switch statement is used in the above program to evaluate the expression a = 1.

  • The switch statement in JavaScript performs a strict check on the value. As a result, the result of the expression does not match case “1.”
  • The switch statement then moves on to the next case. The result of the expressions is the same as in case 1. As a result, the value is one is displayed.
  • The break statement ends the block, and the program’s control flow jumps to the outside of the switch block.

Note: In JavaScript, the switch statement compares the expression’s result to the cases (which must be of the same data type)strictly. That’s why 1 does not match with “1” in the example above.

Let’s use the switch statement to write a program that creates a simple calculator.

Example 3: Simple Calculator using 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: 9
Enter second number: 4
9 + 4 = 13

The user is asked to enter +, -, *, or /, as well as two operands, in the above program. The switch statement then runs cases based on the input from the user.

Switch With Multiple Case in JavaScript

Cases can be grouped together in a JavaScript switch statement to share the same code.

Example 4: Switch With Multiple Cases

// 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:

mango is a fruit.

Multiple cases are grouped in the above programme. The code for all of the grouped cases is the same.

The output would have been the same if the fruit variable had been set to apple or pineapple.

That’s all about the Switch Statement in JavaScript.

You may like continue and break Statements in JavaScript.

Hope this article will guide you to recognize the Switch Statement in JavaScript that you needed and still if you have any problems or queries regarding this, post them in the comments section and we will be glad to assist you.

This Post Has 4 Comments

Leave a Reply