X

We would like to inform you that the domain chat-to.dev, as well as the associated project, is available for purchase. Those interested in negotiating or obtaining more information can contact us at contact@chat-to.dev.

We would like to thank everyone who has followed and supported the project so far.

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

(0) Comments

Welcome to Chat-to.dev, a space for both novice and experienced programmers to chat about programming and share code in their posts.

About | Privacy | Donate
[2026 © Chat-to.dev]