Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs

JavaScript Switch Case

by suresh

JavaScript Switch Case: The JavaScript Else Statement allows us to choose between TRUE or FALSE. When there are more than two options, we use JavaScript Nested If Statement. Say, What if we have ten alternatives to choose?, if we use Nested If, the programming logic will be difficult to understand. Here, JavaScript Switch statements and Else if can handle these types of problems effectively.

The working functionality of the JavaScript Switch Case is almost the same as if condition. As we said before, the Switch statement may have n number of cases. So, the switch case compares the expression value with the values assigned in the case statements. If both the values (expression and case value) match, then statements present in that case statement will execute. Let us see the syntax of switch case in Javascript for better understanding

JavaScript Switch Case Syntax

The syntax of JavaScript Switch Case is as follows:

Switch (expression) 
{
 Case Option 1:
    //Execute these statements when the expression result match Option 1
    break;
 Case Option 2:
    //Execute these statements when the expression result match Option 2
    break;
 Case Option 3:
    //Execute these statements when the expression result match Option 3
    break;
    ......
 Case Option N:
    //Execute these statements when the result of expression match Option N
    break;
 Default:
    //Execute these statements when the result of expression Not matching with any Option
    break;
}

The expression should be either integer or characters (We can write the expression as n/2…. also, but the result should be an integer). Option 1, Option 2…… are constants.

Switch Case allows us to add a default statement. If the variable value is not matching with any of the case statements, then the code present in the default will be executed.

The Break statement is useful to come out from the switch statement. Otherwise, all the statements in the switch condition will execute. Whenever a break encountered, the execution flow would directly come out of the switch.

NOTE: Switch Case will only accept either integers or characters, whereas the Else if statement accepts decimal values (2.3, 3.5, etc.) also.

JavaScript Switch Case Flow Chart

Let us see the flow chart of the Switch case in JavaScript to understand the programming flow.

JavaScript Switch Case flow Chart

JavaScript Switch Example

This program allows us to use Arithmetic Operators to perform arithmetic operations using Switch Case.

Please refer to JavaScript Else Statement, JavaScript Nested If, and Break statement articles in JavaScript.

<!DOCTYPE html>
<html>
<head>
    <title> JavaScript Switch Case </title>
</head>
 <h1> JavaScript Switch Case </h1>
<body>
<script>
    var opertor = '*';
    var number1 = 10, number2 = 2;
    switch (opertor)
    {
        case '+':
            document.write("Addition of two numbers is: " + (number1 + number2));
            break;
        case '-':
            document.write("Subtraction of two numbers is: " + (number1 - number2));
            break;           
        case '*':
            document.write("Multiplication of two numbers is: " + (number1 * number2));
            break;
        case '/':
            document.write("Division of two numbers is: " + (number1 / number2));
            break;
        case '%':
            document.write("Module of two numbers is: " + (number1 % number2));
            break;
        default:
            document.write("<b> You have entered Wrong operator </b>");
            document.write("<br \> Please enter Correct operator such as +, -, *, /, %");
    }
</script>
</body>
</html>

OUTPUT 1: Here we assigned * as operator variable.

JavaScript Switch Case 1

OUTPUT 2: Let us change the operator from valid to the wrong operator to check the default value

JavaScript Switch Case 2

Placed Under: JavaScript

  • SQL DML, DDL, DCL & TCL Cmds
  • SQL NOT EXISTS Operator
  • SQL UPDATE from SELECT
  • SQL AFTER UPDATE Triggers
  • SQL Get Column Names in Table
  • SQL IF ELSE
  • SQL ACID Properties
  • SQL FOR XML PATH
  • Java Two Dimensional Array
  • Java Perfect Number Program
  • Java Count Digits in a Number
  • C Compare Two Strings Program
  • C Print Prime Numbers 1 to 100
  • C program to Reverse a String
  • C Palindrome Number Program
  • C Program for Palindrome String
  • C Remove Duplicate String Chars
  • C Square of a Number Program
  • C Sum and Average of N Number
  • Python Fibonacci Series program
  • Python Area Of Circle Program
  • Python Prime Numbers 1 to 100
  • Python Program for Leap Year
  • Tableau Rank Calculation
  • Tableau Google Maps usage
  • Power BI Format Dates
  • Power BI Top 10 Filters
  • Power BI – Create Hierarchy
  • Power BI DAX Math Functions
  • Learn SSIS in 28 Days
  • SSIS Transformations
  • SSIS Incremental Load
  • SSRS Drill Through Reports
  • SSRS Drill Down Reports
  • R Programming Tutorial
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy