Menu Close

JavaScript switch statement

In this article, you will learn all about the JavaScript switch statement to perform different-2 actions on the basis of different-2 cases. In the previous tutorial, we have seen all about JavaScript if statement with the help of examples.

switch case in JavaScript is used to make a decision-making program in JavaScript code. The swicth statement evaluates an expression and executes the corresponding block of code according to the expression result.

JavaScript switch statement

JavaScript Switch statement is used for decision-making like JavaScript if-else statement. The JavaScript switch statement is responsible for executing different-2 actions on the different conditions.

Syntax


switch(variable/expression) 
{
	case value1:
		// block code 1
		break;
		
	case value2:
		// block code 2
		break;
		
	case value3:
		// block code 3
		break;
		
	case value4:
		// block code 4
		break;
		
	case value5:
		// block code 5
		break;
		
	default:
		// code block of default
		
};

Working:

The switch statement evaluate variable/expression inside parenthesis ().

  1. If the expression equal to value1, its body is executed.
  2. If the expression equal to value2, its body is executed.
  3. This process goes on. If there are no matches, the default body is executed.

Flowchart of the Switch statement in JavaScript

javascript switch statement flowchart

Why should we use switch case ?

Sometimes in a JavaScript program, you want to execute different blocks of code on the basis of the different conditions, Then you can have the best way that is the switch statement. You can perform the same operation using JavaScript if-else statement but it would be complex rather than switch statement.

The JavaScript Switch statement compares the value stored in the variable and executes the block of code according to the value stored in the variable.

You will be clear after reading this article.

Example: Simple program to print a message using switch statement.


let name = 'JavaScript';
switch (name)
{

	case 'R':
		console.log(`Hi ${name}`);
		break;
		
	case 'JavaScript':
		console.log(`Hi ${name}`)
		break;
		
	case 'Python':
		console.log(`Hi ${name}`)
		break;
		
	case 'HTML':
		console.log(`Hi ${name}`);
		break;
		
	default:
		console.log("There is no matches");
		break;
		
};

Output

Hi JavaScript

In the above program,

  1. The switch statement evaluates the expression name = “JavaScript”
  2. The switch statement checks the value, There are no matches in case ‘R’, Then the switch statement goes to the second case.
  3. In the second case, the Switch statement matches with the case “JavaScript”. So Hi JavaScript is displayed.
  4. The break statement terminates the block of code and the control flow of the program jumps out of the switch statement.

Example: Display current day using switch statement.


switch (new Date().getDay())
{
	case 0:
		console.log("Today is Sunday");
		break;
		
	case 1:
		console.log("Today is Monday")
		break;
		
	case 2:
		console.log("Today is Tuesday")
		break;
		
	case 3:
		console.log("Today is wednesday");
		break;
		
		
	case 4:
		console.log("Today is Thursday");
		break;
		
	case 5:
		console.log("Today is Friday");
		break;
		
	case 6:
		console.log("Today is Saturday");
		break;
		
	default:
		console.log("There is no matches");
		break;
		
};

Output

Today is Tuesday

JavaScript switch statement with multiple case

You can group a block of code with multiple cases.


let name = prompt("Enter any fruit name:- ")
switch(name)
{
	case "Apple":
	case "Banana":
	case "Mango":
		console.log(`Fruit name is ${name}`);
		break;
		
	default:
		console.log("There is no matches");
		break;
		
};

Output 1


Enter any fruit name:- Apple
Fruit name is Mango

Output 2


Enter any fruit name:- Grapse
There are no matches

In the above example, we grouped multiple cases with one block of code. If the fruit name entered by the user is Apple, Banana, or Mango, Then the output will be the same.

If the fruit name entered by the user not match the any case, Then body of default is executed.

Conclusion

In this article, you have seen all about JavaScript switch statement with the help of various examples.

Basically, a switch case is used to improving the clarity of the JavaScript code rather than using if-else multiple times. This is the best to execute different parts of the codebase on the variable or expression.

If you like this article, please share and keep visiting for further interesting JavaScript tutorials.

Previous Article
Next Article

Was this post helpful?

Related Posts