Switch Case command in JavaScript

You can use multiple if . else . if statements as in the previous post to make a branch of multiple choices. However, it is not always the best solution, especially when all branches depend on the value of a single variable.

Starting with JavaScript 1.2, you can use a switch command to correctly handle this situation, and it's actually more efficient than repeating if . else if statements .

Implementation diagram

The following diagram explains the switch-case command to work:

Switch Case command in JavaScript Picture 1

Syntax

The goal of a switch command is to provide an expression to estimate and several different commands to execute on the value of the expression. The interpreter checks each case - the case with the value of the expression until a match is found. If there is no match, a default condition - will be used by default .

 switch ( expression ) { case condition 1 : statement ( s ) break ; case condition 2 : statement ( s ) break ; . case condition n : statement ( s ) break ; default : statement ( s ) } 

Break command instructs the end of each specific case. If they are omitted, the interpreter will continue to execute each instruction in each of the following cases.

We will discuss the break command in the Loop control chapter.

For example

Try the following example to execute the switch-case command

  type = "text/javascript" >  
 Set the variable to different value and then try. 

Result:

 Entering block switch 
Good job
Block Exiting switch
Đặt biến vào giá trị khác nhau và thử thử .

Break commands play an important role in switch-case commands. Try the following code using the switch-case command without any break commands.

  type = "text/javascript" >  
 Set the variable to different value and then try. 

Result:

 Entering block switch 
Good job
Pretty good
Passed
Not so good
Failed
Unknown grade
Block Exiting switch
Đặt biến vào giá trị khác nhau và thử thử .

According to Tutorialspoint

Previous lesson: if . else command in JavaScript

Next: The while loop in JavaScript

4 ★ | 1 Vote

May be interested

  • Top site with many good JavaScript exercises to practiceTop site with many good JavaScript exercises to practice
    many people, after taking javascript courses and being equipped with some knowledge of the language they pursue, are eager to improve and cultivate these learned skills. so this article will give you a list of the top 3 websites to actually make javascript.
  • What is JavaScript?What is JavaScript?
    javascript is a programming language of html and web. it is lightweight and most commonly used as part of web pages, but their implementation allows client-side scripts to interact with users and create dynamic websites. it is an interpreted programming language with object-oriented capabilities.
  • The browser makers switch all Web Documentation to Mozilla's portThe browser makers switch all Web Documentation to Mozilla's port
    mozilla, google, microsoft, samsung and w3c all agreed to integrate their document pages into a single source, which is mozilla's mdn port.
  • ! = and! == What is the difference in JavaScript?! = and! == What is the difference in JavaScript?
    javascript includes operators like in other languages. an operator performs some operations on one or more operands (data values) and produces a result. today's article will help readers learn about 2! = and! == operators in javascript.
  • Learn about ES6 in JavascriptLearn about ES6 in Javascript
    es6 refers to version 6 of the ecma script programming language. ecma script is the standard name for javascript and version 6 is the next version after version 5, released in 2011.
  • If, else, switch commands in PHPIf, else, switch commands in PHP
    if, elseif ... else and switch commands are used to control the flow based on different conditions.
  • How to switch users on the Linux command lineHow to switch users on the Linux command line
    suppose for some reason you want to switch to another user account, such as the root account to execute specific commands, the article below tipsmake will guide you how to switch users on the linux command line.
  • Summary of JavaScript exercises with sample codeSummary of JavaScript exercises with sample code
    in order to make your javascript learning easier, tipsmake.com has compiled a number of javascript exercises with sample solutions for you to practice.
  • How to switch from FAT32 to NTFS without losing data with the CMD command in Windows 7,8,10How to switch from FAT32 to NTFS without losing data with the CMD command in Windows 7,8,10
    how to switch from fat32 to ntfs without losing data with the cmd command in windows 7,8,10. gradually replacing the old fat32 format, ntfs has many outstanding advantages such as better security, allowing copying large files, suitable for partitioning the system.
  • JavaScript location in HTML FileJavaScript location in HTML File
    there is flexibility in providing javascript code anywhere in an html document. however, the most preferred ways to include javascript in an html file.