1705233657

Brief example of how to use the switch statement in javascript


The switch statement is an alternative to if/else conditional construct (commonly called a "case statement") and may make the program more readable when handling multiple options. ![switch_statement](https://d1jnx9ba8s6j9r.cloudfront.net/blog/wp-content/uploads/2018/09/switch.png) ```js const day_of_week=Math.floor((Math.random()* 7)+1) // get a random number between 1 and 7 // monday is 1, Tuesday is 2, etc... switch(day_of_week){ case 1: case 2: case 3: case 4: alert("Business hours monday through thurday are from 9am to 10pm"); break; case 5: alert("Business hours on friday are from 9am to 6pm"); break; case 6: alert("Business hours on saturday are from 11am to 3pm"); break; default: alert("we are closed on sundays and holidays"); break; } ``` I would like to know in which cases the use of the switch is more common and why. leave your comments here or create a post here on the site talking about it. thanks

To comment this publication you need to be logged in.