Switch Case in JavaScript – Tutorialpath

Welcome to all our readers on our website at www.tutorialpath.com. As you all know that here on our website we will provide you lots of information related to the latest and newly introduced technology which helps you to get updated with the latest tech. Here you will get all the useful information which is required in this tech world. As you know technology is one of the trending parts of this world. So, here again, we came up with the latest and new tech information which is related to the process of creating the Switch Case in JavaScript. With the help of this article, you will get to know the complete process of how to create Switch Case in JavaScript in a step by step manner. So, simply have a look at this article and grab all the useful information which is going to be very helpful for you.

Switch Case in JavaScript

We’ve learned how to make decisions in JavaScript, using more-and statements in our previous article on if-else statements in JavaScript. We have seen in our previous article that we can use the if-else statement to take action based on some specific situation. If some condition is true then do some task otherwise if the condition is false then execute any other task.

The JavaScript switch statement is used to execute code with multiple expressions. Duplicate case values are not allowed. This is just like the other details we learned in the previous page. But it is convenient if..else..if because it can be used with numbers, characters, etc.

Below is an example of a switch statement with two case statements, and a fallback known as default.

switch (expression)     {
    case abc:    // execute case abc code block
        break;
    case bcd:
    // execute case bcd code block
        break;
    default:
    // execute default code block
}

Let’s see the simple example of a switch statement in javascript.

<script>  

                var grade='B';  

                var result;  

                switch(grade){  

                case 'A':  

                        result="A Grade";  

                        break;  

               case 'B':  

                      result="B Grade";  

                      break;  

               case 'C':  

                     result="C Grade";  

                     break;  

             default:  

                     result="No Grade";  

               }  

              document.write(result);  

     </script>  

Output:

B Grade

Using the switch, we will send a message to the console every day of the week. The program will run in order from top to bottom in search of a match, and once found, the break command will stop the switch block from continuing to evaluate the statements.

// Set the current day of the week to a variable, with 1 being Monday and 7 being Sunday

const day = new Date().getDay();

switch (day) {
    case 1:
        console.log("Happy Monday!");
        break;
    case 2:
        console.log("It's Tuesday. You got this!");
        break;
    case 3:
        console.log("Hump day already!");
        break;
    case 4:
        console.log("Just one more day 'til the weekend!");
        break;
    case 5:
        console.log("Happy Friday!");
        break;
    case 6:
        console.log("Have a wonderful Saturday!");
        break;
    case 7:
        console.log("It's Sunday, time to relax!");
        break;
    default:
        console.log("Something went horribly wrong...");
}

Output:

'Just one more day 'til the weekend!'

JavaScript program to illustrate switch-case

<script type = "text/javascript">
 
    var i = 9;
 
    switch (i) 
    {
       case 0:
           document.write("i is zero.");
           break;
       case 1:
           document.write("i is one.");
           break;
       case 2:
           document.write("i is two.");
           break;
       default:
           document.write("i is greater than 2.");
    }
 
</script></div>

Output:

i is greater than 2.

Conclusion

So, hopefully, we have shared all the basic steps that help you Switch Case in JavaScript.  In this tutorial, you have to learn basic several ways for JavaScript.

Recommended Posts:

  • JavaScript Course | Printing Hello World in JavaScript
  • JavaScript Course | Logical Operators in JavaScript
  • JavaScript Course | Data Types in JavaScript
  • JavaScript Course | Conditional Operator in JavaScript
  • JavaScript Course | Variables in JavaScript
  • JavaScript Course | Objects in JavaScript

 

Leave a Reply

Your email address will not be published. Required fields are marked *