Introduction to JavaScript Switch Cases

undefined or mostly null.
Search for a command to run...

undefined or mostly null.
Computers are dumb machines. They are not intelligent like humans. We have t teach them about every functionality as by is essayshark reliable recommendation. And for that, we have to write codes. We write codes for almost every functionality. And we write these codes in a computer-friendly language like javascript.
In this series, I will share articles related to best practices for building and deploying applications on the web with the JavaScript and Python programming languages.
Recently I discovered a new trend, a powerful web technology, one that every Web Developer should know, yeah I said every! I first heard about PWA’s a few months back, and at first, it was weird trying to understand the concept and then trying to lea...
I wrote this article in December 2023 for an interview screening task and thought to share it today while clearing my drafts for some new content prep, just in case it's still helpful to someone out t

Hey! I’m super excited to announce that I have joined the Secretariat Team at the Digital Public Goods Alliance. In this role, I leverage my passion for open source and technical background to assess DPG applications, provide technical support and ad...

Creative designs have become more important than ever in the software ecosystem today with many industries and end-consumers having several use cases that require them to offer design editing solutions to either designers or end-consumers. Every busi...

With the rise of artificial intelligence (AI) and large language models (LLMs), it has become easier to solve different human problems than ever before. Even consumers with little to no technical expertise can benefit from AI. Humans can now automate...

Some years ago, GitHub introduced the new Profile README feature that allowed GitHub users to pin a markdown file on their profile using a special repository named after their GitHub username. Since then, developers have used this file as a quick por...

In this short article, I will introduce you to JavaScript switch cases and how to use them with practical examples. PS: This article is not for those without any JavaScript knowledge; it is expected that you're taking some tutorial or course and you just got to the switch cases section. This article will explain better with more practical examples to help you understand switch cases in depth.
A switch statement can basically replace multiple if checks in JavaScript. It gives a more descriptive way to compare a value with multiple variants.
The switch has one or more case blocks and an optional default case.
switch(x) {
case 'value1': // if (x === 'value1')
//code here
[break]
case 'value2': // if (x === 'value2')
//code here
[break]
default:
//code here
[break]
}
x is checked for strict equality to the value from the first
case (that is, value1) then to the second (value2) and so on.switch starts to execute the code starting from the
corresponding case, until the nearest break(or until the end of switch).default code is executed (if it exists).The switch statement can be used for multiple branches based on a number or
string:
switch (movie) {
case 'play':
playMovie();
break;
case 'pause':
pauseMovie();
break;
default:
doNothing();
}
If you don’t add a break statement, the execution will "fall through" to the
next level. It's essential that you deliberately label the fall through with a
comment if you really meant it to aid debugging:
switch (movie) {
case 'play': // fallthrough
case 'pause':
pauseMovie();
break;
default:
doNothing();
}
The default clause is optional. You can have expressions in both the switch part
and the cases if you like; comparisons take place between the two using the
=== operator:
switch (3 + 7) {
case 5 + 5:
correct();
break;
default:
neverhappens();
}
let average = 2 + 6;
switch (average) {
case 4:
alert( 'Too small' );
break;
case 8:
alert( 'Exactly!' );
break;
case 10:
alert( 'Too large' );
break;
default:
alert( "Incorrect values!" );
}
Here the switch starts to compare average from the first case variant, that
is 4. The match fails.
Then 8. That’s a match, so the execution starts from case 8 until the nearest
break.
If there is no break, then the execution continues with the next case without any checks.
Here is an example without break:
let average = 2 + 6;
switch (average) {
case 4:
alert( 'Too small' );
case 8:
alert( 'Exactly!' );
case 10:
alert( 'Too big' );
default:
alert( "Incorrect values!" );
}
In the example above, we’ll see the sequential execution of three alerts:
alert( 'Exactly!' );
alert( 'Too big' );
alert( "Incorrect values!" );
The getDay() method returns the weekday as a number between 0 and 6.
(Sunday=0, Monday=1, Tuesday=2 , Wednesday=3, Thursday=4, Friday=5, Saturday=6)
This example uses the weekday number to calculate the weekday name:
switch (new Date().getDay()) {
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";
}
The result of the day will be the current weekday in day format.
PS: This would change according to when you’re reading this article.
I wrote this article on 01/04/2019, which is a Friday, so the result would be:
Friday
The default keyword specifies the code to run if there is no case match, more like an else statement:
switch (new Date().getDay()) {
case 6:
text = "Today is Saturday";
break;
case 0:
text = "Today is Sunday";
break;
default:
text = "It's not the weekend yet!";
}
The result of the text will be: It's not the weekend yet!.
The default case does not have to be the last case in a switch block:
switch (new Date().getDay()) {
default:
text = "Its not weekend yet!";
break;
case 6:
text = "Today is Saturday";
break;
case 0:
text = "Today is Sunday";
}
If the
defaultis not the last case in the switch block, remember to end the default case with a break.
There are so many practical examples of switch cases. You can head here to learn more about switch cases.
Thanks for reading!