1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Introduction to JavaScript Conditionals - Switch Statements

Introduction to JavaScript Conditionals - Switch Statements

Contents

keyboard_tab
JavaScript Conditions - Switch Statements
Introduction to JavaScript Switch Statements
Overview
Difficulty
Beginner
Duration
4m
Students
20
Ratings
5/5
starstarstarstarstar
Description

This practical course explores JavaScript Conditionals (Switch statements). You will learn what they are and how to use them.

Learning Objectives

  • Learn the difference in structure between an if statement and a switch statement
  • Learn what is the switch expression
  • Learn how to create a case clause
  • Learn how to create a default case
  • Learn the use of the break statement in the switch statement

Intended Audience

This course is intended for anyone who wants to learn about JavaScript switch statements.

Prerequisites

Anyone with an interest in JavaScript switch statements or who wants to improve their knowledge of JavaScript in general.

Transcript

Introduction to JavaScript conditionals. Switch statements. On the screen, I have an if statement chain. I'm gonna convert this over to a switch statement. I'm gonna begin by commenting out the if statement chain. I will type switch, then parentheses and inside of the parentheses goes the expression. And the expression is the value that is evaluated against what is called the case clause.

For this switch statement the expression will be the myCar variable. Inside of the curly brackets, I will write the first case clause. Case, in quotes Toyota, colon. Colon is the separator between the case and the block of code to be executed. And whatever code is underneath the colon is what's gonna be executed if the expression matches the case. Since I'm refactoring the if statement to be the switch statement, I'm gonna copy the console log under if my car triple equals Toyota, and paste it under the case.

Now comparing the first case statement with the first if statement, if my car triple equals Toyota is the same as the myCar expression being evaluated against the case Toyota, both will return false and neither will execute the corresponding block of code, which in this case is the console log.

Now, as part of the switch statement block of code, I'm gonna add a break. Break, semi-colon. And a break will stop the switch statement from continuing on to the next clause if the case statement evaluates to true. Now I'm gonna speed up the video for this portion of code. I need to repeat the process and make two additional case clauses for each else-if statement from above. With the current value of the expression and however switch statement is structured, none of the case clauses will evaluate true.

The if statement has an else statement that triggers when all previous conditions do not evaluate to true. The switch statement has something similar. That is called the default case. I will type default colon, and copy and paste the corresponding console log from the else statement above.

Now, because I wrote this as the final case, there is no need for me to add a break statement. Now I could put the default in any case position, including the first one. And if I were to do that, I would include the break statement. Now I will execute this. And in the console to the right, "I don't own that car" is displaying.

Now, one other thing about the break statement use case. In each of these case clauses, I'm using console log to display a message. When console log is executed, it does not break the execution cycle. Underneath the Toyota case, if I were to replace the console log with return, the break statement would no longer be necessary. And that's it. Thanks for watching, at Cloud Academy.

About the Author
Avatar
Farish Kashefinejad
Full-Stack Development Content Creator
Students
1877
Labs
16
Courses
39
Learning Paths
4

Farish has worked in the EdTech industry for over six years. He is passionate about teaching valuable coding skills to help individuals and enterprises succeed.

Previously, Farish worked at 2U Inc in two concurrent roles. Farish worked as an adjunct instructor for 2U’s full-stack boot camps at UCLA and UCR. Farish also worked as a curriculum engineer for multiple full-stack boot camp programs. As a curriculum engineer, Farish’s role was to create activities, projects, and lesson plans taught in the boot camps used by over 50 University partners. Along with these duties, Farish also created nearly 80 videos for the full-stack blended online program.

Before 2U, Farish worked at Codecademy for over four years, both as a content creator and part of the curriculum experience team.

Farish is an avid powerlifter, sushi lover, and occasional Funko collector.

Covered Topics