Switch case JavaScript Statement | Example String, Number & 2 values

Switch case in javascript used for decision-maker logic based on different conditions. In Switch case where you need to pass the value and this value will match anyone’s condition and execute some task (code of bock).

A Switch case statement is more convenient over if-else statements and an easy way to dispatch execution to different parts of code based on the value of the expression. Compare to if-else statements its more efficient and code look clean. Where using multiple if-else statement code look messy.

Switch case JavaScript Statement

How Does the Switch case Statement work?

  • A switch expression starts once.
  • The value of express will be compared with every case one by one.
  • If any case value matched then its code block is executed.

Important Terms in Switch Case:

  • Break Keyword: This will stop the execution of inside the block.
  • Default Keyword: This is the default code if there is no case match:


Syntax of the JS switch case statement.


Here is an example of a switch case in javascript for numbers. In the example, we have to pass the value between 0-6 and match case will return day.


example of a switch case in javascript

Q: How to use switch case javascript string?

Answer: The below code shows how to use a switch statement with a string value.

Q: Does Javascript switch case can have multiple cases or 2 values?

Answer: Yes, in the JS Switch case you can use 2 or multiple values in one case. In the example, you can try with different values (1, 2, 3) and the result will be an alert box with a message.


Javascript switch multiple cases or 2 values

Q: How to use a case/switch statement with two variables?

Answer: if you want switch case with two parameters in js then use if condition statement. See below example:

source: https://stackoverflow.com/questions/41161658/switch-case-js-two-parameters

Do comment if you have any doubts and suggestions on this tutorial.

Note: The Switch case javascript example are tested on Safari browser (Version 12.0.2) and Chrome.
OS: macOS 10.14 Mojave
Code: HTML 5 Version

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.