Friday , 17 November 2017
Home / Programming / JavaScript / Switch Statements – JavaScript Tutorial
Switch Statements
Switch Statements

Switch Statements – JavaScript Tutorial

This tutorial is part of the JavaScript tutorial series for beginners. To view the whole list of tutorials available and the order in which you should follow them, then click here.

Switch statements are similar to if statements. They allow you to check if a certain condition is true and then execute a certain block of code accordingly. Let’s take a look at how to create switch statements.

Creating Switch Statements

Clear your script and add this bit of code.

We have a variable named num and it’s value is 5. We then use the switch keyword to start our switch statement. The switch statement searches for a match instead of comparing 2 values. The value passed into the parenthesis is the value that you want to match.

Update your switch statement to this.

A couple of things are going on here. First, we use the case keyword to create all the possible values that can match the value in the switch statement.  After your case keyword, is the value that you want to match with the switch”s value. After that you insert a colon. Any code after this is then executed. For formatting reasons, we indent all code that will be executed for readability.

You can enter as many lines of code as you want. After you run everything you want to run, then you break that case. The break keyword is used to stop the switch statement from continuing to search for a match. Unlike if-else statements, switch statements goes all the way through. So, you could end up executing more than one case. So, by using break, you’re stopping this from happening.  Remember, this will only run if a case is matched. You can create as many cases as you want.

If all else fails, JavaScript will search for a default block of code to run. You use the default keyword to do this. You don’t have to enter a match value because it’s the default state to run. You also don’t have to break the switch statement cause it’s already done for you.

Switch vs If Statements

You can use either of these to perform checks. I personally don’t use switch statements, but there are developers that do. Knowing both of these will help you in the long run. You can use switch statements and you’ll be fine. Same goes for if statements. For this series, we’ll be using if statements.

About Jasko Koyn

Check Also

Try-Catch Statement

Try-catch Statement – Advanced JavaScript Tutorial

The try-catch statement is a unique way to handle errors. When errors occur, usually it …

Leave a Reply

Your email address will not be published. Required fields are marked *

ULTIMATE WORDPRESS DEVELOPMENT COURSE
Want to learn how to create your own custom WordPress plugins and themes? Check out my course!
Get 93% off my course. You'll also learn how to integrate WooCommerce and BuddyPress into your projects. This is a limited time offer
Yes, I would like to get 93% off your course!
No Thanks!
close-link