JavaScript

What are Switch Case statements of JavaScript?

What are Switch Case statements of JavaScript?

In a previous article on How to use if-else - Statement in JavaScript? we can see that conditional rendering is possible with the help of if-else statements. There is also a different method which makes It more convenient and easier to use, this is the Switch Case.

The switch case in JavaScript makes it easier to handle stuff like if-else and without using the if-else statements.

The switchstatement evaluates an expression, matching the expression's value to a case clause, and executes statements associated with that case, as well as statements in cases that follow the matching case. – MDN Web Docs 

In easy language, we can justify it by thinking of a situation that will handle hundreds of cases/conditions. Writing down code for each case with if-else is a bit less convenient whereas with switch case it becomes easier to perform individual tasks after running tests to each case.

Syntax:

switch(expression){

                case x : statement;

                                break;

                case y : statement;

                                break;

                case z : statement;

                                break;

                     ….

                default:

                               statementDefault;

}




// x,y,z can be conditions, values, numbers

In the above statement, we will pass an expression inside the switch() which will be checked in the cases. In case if any case matches the expression inside the switch() then the statement in that particular case will execute. Then there is also break;  after every case except for the default.

The break statements indicate the end of a particular case. If they were omitted, the interpreter would continue executing each statement in each of the following cases.

At last, there is the default:  which is a clause This clause executes when the expression matches none of the provided cases.

The evaluation takes place in the following way:

  • The switch expression gets evaluated once.
  • The value of the expression is then compared with each value of the case.
  • If there is a match, the respective code inside the case is executed.
  • If there is no match, the default code is executed.

Example

const food = ‘Dim Sums’

swicth(food){




                case ‘Rolls’ : console.log(‘Price is not yet decided’);

                break;




                case ‘Taco’ : console.log(‘Price is not yet decided’);

                break;




                case ‘Dim Sums’ : console.log(‘Price is Rs.50/-‘);

                break;




                case ‘PIzza’ : console.log(‘Price is not yet decided’);

                break;




                default:

                console.log(‘We can not recognize the item’);




}


The expected outcome/ result of the above statement is Price is Rs.50/-, just in case if we remove the break statement.

const food = ‘Dim Sums’




swicth(food){




                case ‘Rolls’ : console.log(‘Price is not yet decided’);

                break;




                case ‘Taco’ : console.log(‘Price is not yet decided’);

                break;




                case ‘Dim Sums’ : console.log(‘Price is Rs.50/-‘);

                // No break statement




                case ‘PIzza’ : console.log(‘Price is not yet decided’); //No break statement in the previous statement so it will run too.

                break; // it encounters this break so will not continue into next case







                default:

                console.log(‘We can not recognize the item’);




}

The expected result will be Price is Rs.50/- and Price is not yet decided.

Case Grouping

This is also known as common code blocks or Multi-case single operation. This is basically defining a single code to execute for different individual cases by grouping them

Example

const food = ‘Dim Sums’




swicth(food){




                case ‘Rolls’ : //grouped cases of 3

                case ‘Taco’ : //grouped cases of 3

                case ‘PIzza’ : console.log(‘Price is not yet decided’);

                break;




                case ‘Dim Sums’ : console.log(‘Price is Rs.50/-‘);

                break;




                default:

                console.log(‘We can not recognize the item’);




}

In the above code, the cases of ‘Rolls’, ‘Taco’, and ‘Pizza´ are grouped together and will display the same message that is Price is not yet decided.

Shibam Dhar

A sophomore web-dev freak always enthusiastic to learn more and explore different technologies and domains. Currently pursuing Bachelor of technology in CSE. Fascinated by web development. A hard worker and a self learner, who's goal is be a valuable asset. Always fascinated by communities and ready to contribute to it. I am always looking for opportunities enabling me to learn exciting technologies to boost my passion for programming.