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

Switch case in javascript used for decision-maker logic based on different conditions. In the 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 than 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 it’s more efficient and the code looks 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 inside the block.
  • Default Keyword: This is the default code if there is no case match:

Syntax

Syntax of the JS switch case statement.

switch(expression) {
  case x:
    // code block
    break;
  case y:
    // code block
    break;
  default:
    // code block
}

Example

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 the match case will return day.

<!DOCTYPE html>
<html>
<body>

<p id="day"></p>

<script>
var day;
switch (1) {
  case 0:
    day = "Sunday";
    break;
  case 1:
    day = "Monday";
    break;
  case 2:
    day = "Tuesday";
    break;
  case 3:
    day = "Wednesday";
    break;
  case 4:
    day = "Thursday";
    break;
  case 5:
    day = "Friday";
    break;
  case  6:
    day = "Saturday";
}
document.getElementById("day").innerHTML = "Today is " + day;
</script>

</body>
</html>

Output:

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 the 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.

<!DOCTYPE html>
<html>
<body>


<script>
	varName = 2;
switch (varName)
{
   case 1:
   case 2:
   case 3: 
       alert('Hey');
       break;

   default: 
       alert('Default case');
}
</script>

</body>
</html>

Output:

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:

var treshhold_two = 0;

function Test(attack, ratio) {
  if(attack == 0,01) {
     switch (ratio) {
        case 2:
          treshhold = 2798,6;
          break;
        case 4:
          treshhold = 3678,16;
          break;
        case 6:
          treshhold = 5757,12;
          break;
       }
   }
   else {           
      switch (attack) {
        case 0,03:
              if(ratio==2) treshhold = -5,75712;                 
              if(ratio==4) treshhold = -5,75712 * 1,1;                             
              if(ratio==6) treshhold = -5,75712 * 0,96;                  
              break;
        ...
      }          
  }
}

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.