Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • JavaScript - Get Started
  • What is JavaScript
  • Setup JavaScript Dev Environment
  • HTML <script> Tag
  • JavaScript - Syntax
  • JavaScript - Popup Message
  • JavaScript - Variables
  • JavaScript - Operators
  • JavaScript - Data Types
  • JavaScript - String
  • JavaScript - Numbers
  • JavaScript - Boolean
  • JavaScript - Object
  • JavaScript - Date
  • JavaScript - Date Methods
  • JavaScript - Array
  • JavaScript - Array Methods
  • JavaScript - null and undefined
  • JavaScript - Function
  • JavaScript - if condition
  • JavaScript - switch
  • JavaScript - for loop
  • JavaScript - while loop
  • JavaScript - Scope
  • JavaScript - eval
  • JavaScript - Error Handling
  • JavaScript - strict mode
  • JavaScript - Hoisting
  • Define JS Class
  • JS Object In Depth
  • this Keyword
  • new Keyword
  • Prototype
  • Inheritance
  • Closure
  • IIFE
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

JavaScript switch

The switch is a conditional statement like if statement. Switch is useful when you want to execute one of the multiple code blocks based on the return value of a specified expression.

Syntax:

switch(expression or literal value){
    case 1:
        //code to be executed
      break;
    case 2:
        //code to be executed
        break;
    case n:
        //code to be executed
        break;
    default:
        //default code to be executed 
        //if none of the above case executed
}

Use break keyword to stop the execution and exit from the switch. Also, you can write multiple statements in a case without using curly braces .

As per the above syntax, switch statement contains an expression or literal value. An expression will return a value when evaluated. The switch can includes multiple cases where each case represents a particular value. Code under particular case will be executed when case value is equal to the return value of switch expression. If none of the cases match with switch expression value then the default case will be executed.

Example: switch Statement
var a = 3;

switch (a) {
    case 1:
        alert('case 1 executed');
        break;
    case 2:
        alert("case 2 executed");
        break;
   case 3:
        alert("case 3 executed");
        break;
    case 4:
        alert("case 4 executed");
        break;
    default:
        alert("default case executed");
}
Try it

In the above example, switch statement contains a literal value as expression. So, the case that matches a literal value will be executed, case 3 in the above example.

The switch statement can also include an expression. A case that matches the result of an expression will be executed.

Example: switch Statement
var a = 3;

switch (a/3) {
    case 1:
        alert("case 1 executed");
        break;
    case 2:
        alert("case 2 executed");
        break;
    case 3:
        alert("case 3 executed");
        break;
    case 4:
        alert("case 4 executed");
        break;
    default:
        alert("default case executed");
}
Try it

In the above example, switch statement includes an expression a/3, which will return 1 (because a = 3). So, case 1 will be executed in the above example.

The switch can also contain string type expression.

Example: switch with String Type Case
var str = "bill";

switch (str) 
{
    case "steve":
        alert("This is Steve");
    case "bill":
        alert("This is Bill");
        break;
    case "john":
        alert("This is John");
        break;
    default:
        alert("Unknown Person");
        break;
}
Try it

Multiple cases can be combined in a switch statement.

Example: Combined switch Cases
var a = 2;

switch (a) {
    case 1:
    case 2:
    case 3:
        alert("case 1, 2, 3 executed");
        break;
    case 4:
        alert("case 4 executed");
        break;
    default:
        alert("default case executed");
}
Try it
Points to Remember :
  1. The switch is a conditional statement like if statement.
  2. A switch statement includes literal value or is expression based
  3. A switch statement includes multiple cases that include code blocks to execute.
  4. A break keyword is used to stop the execution of case block.
  5. A switch case can be combined to execute same code block for multiple cases.
TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.