Conditional statements form the backbone of any logic-driven programming language.
Popular conditions make logic constructs easier to write - but what if you had say, 10 conditions to check?
Writing that many nested statements wouldn't be a great solution!
Fortunately, the statement is a useful feature for this. It is found in most programming languages, including JavaScript!
Let’s learn about the JS switch statement and how to use it.
The JS statement is used to evaluate expressions and execute a subsequent set of code blocks if the result matches the case.
The syntax is similar to an condition:
switch(expression) {
case 1:
// execute code
break;
case 2:
// execute code
break;
case 2:
// execute code
break;
case 3:
// execute code
break;
default:
// execute fall-back code
}
As you can see, the syntax is very readable - almost natural language-ish! That’s what makes the statement a breeze to work with.
Now, let’s dive deeper into the syntax and relevant keywords:
Pretty handy, right? 😎
But, as Linus Torvalds famously said,
So, let's dive into an example to see how it all comes together.
Let's explore a JavaScript string example:
const dayOfWeek = ‘Monday’
switch (dayOfWeek) {
case ‘Monday’:
console.log(‘The best work day of the week!’);
break;
case ‘Tuesday’:
console.log(‘The day of endless meetings!’);
break;
case ‘Wednesday’:
console.log(‘Halfway there!’);
break;
case ‘Thursday’:
console.log(‘Monday - one day, Tuesday - two day, Wednesday - huh? What day? THUUURSDAYYY’);
break;
case ‘Friday’:
console.log(‘The weekend’s already begun, woohoo!’);
break;
case ‘Saturday’:
console.log(‘Off the planet!’);
break;
case ‘Sunday’:
console.log(‘Wasted the day anticipating the Monday blues’);
break;
default:
console.log(‘You seem to have a different calendar there!’);
}
In this JavaScript switch string example, we evaluate the variable against different string cases, i.e. days of the week.
If one of the days of the week are a match, that particular statement will be executed.
If none of the days of the week match, then the case will execute.
Think of the case as a fallback for the scenario when
The keyword can also evaluate expressions! This is very useful when working with a range of data.
A great use-case of this would be that of evaluating grades. For each of marks scored on a test by a student, there is a specific grade that can be attached to it.
For example:
switch (grade) {
case grade >= 85: // data range
console.log(‘You scored an A’);
console.log(‘Congratulations!’);
break;
case grade >= 60: // data range
console.log(‘You scored a B’);
console.log(‘Congratulations!’);
break;
//….
…
//
default:
console.log(‘Please contact your teacher’);
}
Did you notice in the above code that we have logged multiple times?
There are some situations where we may need to execute the same block of code in multiple cases - but repeating this same code over and over is not optimal!
This is where the multiple cases feature of a JS statement can help:
switch(marks) {
case 100:
case 90:
case 80:
console.log(‘Congratulations! You passed with Distinction’);
break;
case 70:
case 60:
console.log(‘Congratulations! You passed First Class with Distinction’);
break;
//..
..
//
}
Fun fact - did you know that the statement goes as far back as the 1950s to the times of languages like Lisp used in mainframes? 😯
In that context, the statement is quite literally a time-tested, handy feature in programming!
It helps solve a variety of match-based expression evaluations and does so without abstracting away any of the logic.
Congratulations for completing this resource! Mark it as completed to track your progress.