Skip to content

Buzzingz

Unleashing the Power of Code

  • HTML
  • CSS
  • JavaScript
  • Toggle search form

JavaScript Switch Statement

Posted on April 2, 2023April 2, 2023 By shani No Comments on JavaScript Switch Statement

In JavaScript, the switch statement is used to evaluate an expression and execute different code blocks based on different cases.

Syntax

switch (expression) {
  case value1:
    // code block
    break;
  case value2:
    // code block
    break;
  default:
    // code block
}

The expression is evaluated once, and the value of the expression is compared with each case value. If there is a match, the code block associated with that case is executed. If there is no match, the code block associated with the default case is executed (if there is one).

The break statement is used to exit the switch statement and prevent the code from executing the code in the other cases.

Example

let day = 2;
let dayName;
switch (day) {
  case 1:
    dayName = "Sunday";
    break;
  case 2:
    dayName = "Monday";
    break;
  case 3:
    dayName = "Tuesday";
    break;
  case 4:
    dayName = "Wednesday";
    break;
  case 5:
    dayName = "Thursday";
    break;
  case 6:
    dayName = "Friday";
    break;
  case 7:
    dayName = "Saturday";
    break;
  default:
    dayName = "Invalid day";
}
console.log(dayName); // "Monday"

In this example, the day variable is assigned the value of 2. The switch statement then evaluates the day variable and compares it to each case value. Since day equals 2, the code block associated with the case 2: label is executed, which assigns the dayName variable the value of "Monday". The break statement is then used to exit the switch statement.

Multiple Cases

It is possible to have multiple cases share the same code block in a switch statement. To do this, separate the case values with commas:

let fruit = "orange";
let color;
switch (fruit) {
  case "apple":
  case "cherry":
  case "strawberry":
    color = "red";
    break;
  case "banana":
  case "lemon":
    color = "yellow";
    break;
  case "orange":
    color = "orange";
    break;
  default:
    color = "unknown";
}
console.log(color); // "orange"

In this example, the code block for the case "apple":, case "cherry":, and case "strawberry": labels is the same.

FAQs

  1. What is a switch statement in JavaScript? A switch statement is a control statement in JavaScript that evaluates an expression and executes different code blocks based on different cases.
  2. How does a switch statement work in JavaScript? The switch statement evaluates an expression and compares the value of the expression to each case value. If there is a match, the code block associated with that case is executed. If there is no match, the code block associated with the default case is executed (if there is one).
  3. What is the syntax of a switch statement in JavaScript? The basic syntax of a switch statement in JavaScript is switch (expression) { case value1: // code block break; case value2: // code block break; default: // code block }.
  4. Is it possible to use a switch statement with strings? Yes, you can use a switch statement with strings in JavaScript. Starting from ECMAScript 6, you can use template literals to specify the cases as strings, like this
let fruit = 'apple';
switch (fruit) {
  case `apple`:
    console.log(`I like apples`);
    break;
  case `banana`:
    console.log(`I like bananas`);
    break;
  default:
    console.log(`I don't like ${fruit}`);
    break;
}
Javascript

Post navigation

Previous Post: JavaScript if, else, and else if
Next Post: JavaScript For Loop

Leave a Reply Cancel reply

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

JavaScript Tutorial

  • JavaScript Tutorial
  • JavaScript Where To
  • JavaScript Output
  • JavaScript Statements
  • JavaScript Syntax
  • JavaScript Comments
  • JavaScript Variables
  • JavaScript Let
  • JavaScript Const
  • JavaScript Operators
  • JavaScript Arithmetic
  • JavaScript Assignment
  • JavaScript Data Types
  • JavaScript Functions
  • JavaScript Objects
  • JavaScript Events
  • JavaScript Strings
  • JavaScript String Methods
  • JavaScript String Search
  • JavaScript Template Literals
  • JavaScript Numbers
  • JavaScript BigInt
  • JavaScript Number Methods
  • JavaScript Number Properties
  • JavaScript Arrays
  • JavaScript Array Methods
  • JavaScript Sorting Arrays
  • JavaScript Array Iteration
  • JavaScript Array Const
  • JavaScript Date Objects
  • JavaScript Date Formats
  • JavaScript Get Date Methods
  • JavaScript Set Date Methods
  • JavaScript Math Object
  • JavaScript Random
  • JavaScript Booleans
  • JavaScript Comparison and Logical Operators
  • JavaScript if, else, and else if
  • JavaScript Switch Statement
  • JavaScript For Loop
  • JavaScript For In
  • JavaScript For Of
  • JavaScript While Loop
  • JavaScript Break and Continue
  • JavaScript Iterables
  • JavaScript Sets
  • JavaScript Maps
  • JavaScript typeof
  • JavaScript Type Conversion
  • JavaScript Bitwise Operations
  • JavaScript Regular Expressions
  • JavaScript Operator Precedence
  • JavaScript Errors
  • JavaScript Scope
  • JavaScript Hoisting: Understanding the Basics
  • JavaScript Use Strict: The Importance of Strict Mode in JavaScript
  • The JavaScript this Keyword
  • JavaScript Arrow Function: A Concise and Comprehensive Guide
  • JavaScript Classes: Understanding the Basics
  • JavaScript Modules
  • JavaScript JSON
  • JavaScript Debugging
  • JavaScript Style Guide
  • JavaScript Best Practices
  • JavaScript Common Mistakes: How to Avoid Them
  • JavaScript Performance: Techniques for Improving Your Code
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions

Copyright © 2023 Buzzingz.

Powered by PressBook WordPress theme