Home Web Front-end JS Tutorial Detailed explanation of switch usage and precautions in JavaScript

Detailed explanation of switch usage and precautions in JavaScript

Jul 18, 2017 pm 02:49 PM
javascript switch Precautions

Syntax

The basic syntax of the switch statement gives an expression to evaluate and calculate several different statements based on the value of the expression. The interpreter checks each case for the expression's value until it finds a match. If there is no match, the default condition will be used.


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

The break statement instructs the interpreter to end under specific circumstances. If they are omitted, the interpreter will continue to execute each statement in each of the following cases.

We will explain the break statement in the loop control chapter.
Example:

The following example illustrates a basic while loop:


##

<script type="text/javascript">
<!--
var grade=&#39;A&#39;;
document.write("Entering switch block<br />");
switch (grade)
{
 case &#39;A&#39;: document.write("Good job<br />");
      break;
 case &#39;B&#39;: document.write("Pretty good<br />");
      break;
 case &#39;C&#39;: document.write("Passed<br />");
      break;
 case &#39;D&#39;: document.write("Not so good<br />");
      break;
 case &#39;F&#39;: document.write("Failed<br />");
      break;
 default: document.write("Unknown grade<br />")
}
document.write("Exiting switch block");
//-->
</script>
Copy after login

This will produce the following results:


Entering switch block
Good job
Exiting switch block
Copy after login

Example:

Consider such a situation, if you do not use the break statement:


<script type="text/javascript">
<!--
var grade=&#39;A&#39;;
document.write("Entering switch block<br />");
switch (grade)
{
 case &#39;A&#39;: document.write("Good job<br />");
 case &#39;B&#39;: document.write("Pretty good<br />");
 case &#39;C&#39;: document.write("Passed<br />");
 case &#39;D&#39;: document.write("Not so good<br />");
 case &#39;F&#39;: document.write("Failed<br />");
 default: document.write("Unknown grade<br />")
}
document.write("Exiting switch block");
//-->
</script>
Copy after login

This will produce the following results:


Entering switch block
Good job
Pretty good
Passed
Not so good
Failed
Unknown grade
Exiting switch block
Copy after login

Javascript switch usage precautions

<script>
var t_jb51_net = 65;
switch (t_jb51_net) {
case &#39;65&#39;:
alert("字符串65。jb51.net");
break;
}
</script>
Copy after login

You will find that no dialog box pops up and alert is not executed.

Cause analysis:

What needs to be made clear here is that switch uses the congruent sign "===" when making judgments. When comparing congruent signs, the first thing to look at is the data type. It's not the same, but here, t_jb51_net is of type Number, and '65' is String.

The following code will pop up the dialog box:

<script>
var t_jb51_net = 65;
switch (t_jb51_net) {
case 65:
alert("数字65。jb51.net");
break;
}
</script>
Copy after login

The above is the detailed content of Detailed explanation of switch usage and precautions in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Can Elden's Ring be played on switch? Can Elden's Ring be played on switch? Mar 11, 2024 am 11:31 AM

Can Elden's Ring be played on the switch? As a very charming action RPG game, many friends may not know whether it can be played smoothly on the switch platform. The answer is that it cannot be played at the moment. accomplish. Can Ring of Elden be played on switch? Answer: It cannot be played on switch. This highly anticipated Souls series role-playing action game has been officially released. Players can purchase it on PC, PS4/5 and Xbox Series eX|S/XboxOne and experience it immediately. Many friends who own a switch may still be eager to enjoy this game on the NS, but unfortunately, there is no switch version of the game. According to the official website configuration requirements, the game configuration is relatively high, and sw

Introduction to matters needing attention during the Mingchao test Introduction to matters needing attention during the Mingchao test Mar 13, 2024 pm 08:13 PM

During the Mingchao test, please avoid system upgrades, factory resets, and parts replacement to prevent information loss and abnormal game login. Special reminder: There is no appeal channel during the testing period, so please handle it with caution. Introduction to matters needing attention during the Mingchao test: Do not upgrade the system, restore factory settings, replace equipment components, etc. Notes: 1. Please upgrade the system carefully during the test period to avoid information loss. 2. If the system is updated, it may cause the problem of being unable to log in to the game. 3. At this stage, the appeal channel has not yet been opened. Players are advised to choose whether to upgrade at their own discretion. 4. At the same time, one game account can only be used with one Android device and one PC. 5. It is recommended that you wait until the test is completed before upgrading the mobile phone system or restoring factory settings or replacing the device.

How to start a live broadcast on Douyin for the first time? What should you pay attention to when broadcasting live for the first time? How to start a live broadcast on Douyin for the first time? What should you pay attention to when broadcasting live for the first time? Mar 22, 2024 pm 04:10 PM

With the rise of short video platforms, Douyin has become an indispensable part of many people's daily lives. Live broadcasting on Douyin and interacting with fans are the dreams of many users. So, how do you start a live broadcast on Douyin for the first time? 1. How to start a live broadcast on Douyin for the first time? 1. Preparation To start live broadcast, you first need to ensure that your Douyin account has completed real-name authentication. You can find the real-name authentication tutorial in &quot;Me&quot; -&gt; &quot;Settings&quot; -&gt; &quot;Account and Security&quot; in the Douyin APP. After completing the real-name authentication, you can meet the live broadcast conditions and start live broadcast on the Douyin platform. 2. Apply for live broadcast permission. After meeting the live broadcast conditions, you need to apply for live broadcast permission. Open Douyin APP, click &quot;Me&quot;-&gt;&quot;Creator Center&quot;-&gt;&quot;Direct

Is Switch2 compatible with Switch cartridges? Is Switch2 compatible with Switch cartridges? Jan 28, 2024 am 09:06 AM

Switch2 is a new model announced by Nintendo at Gamescom 2023. Some players are worried about whether there will be compatibility issues between the new model and the cartridges of previous versions. Let’s take a look. Is switch2 compatible with switch cassette? Answer: switch2 is not compatible with switch cassette. Introduction of Switch 2 cartridges According to information from Nintendo’s production chain company, Switch 2 may use 64GB cartridges. Because it has better performance and supports more 3A game masterpieces, it requires a larger cartridge capacity. Because many game works need to be castrated and compressed before they can be stuffed into a game cartridge. Moreover, Switch cartridges are prone to copying game content, so replace them with new cartridges.

Frequently Asked Questions and Notes: Using MyBatis for Batch Query Frequently Asked Questions and Notes: Using MyBatis for Batch Query Feb 19, 2024 pm 12:30 PM

Notes and FAQs on MyBatis batch query statements Introduction MyBatis is an excellent persistence layer framework that supports flexible and efficient database operations. Among them, batch query is a common requirement. By querying multiple pieces of data at one time, the overhead of database connection and SQL execution can be reduced, and the performance of the system can be improved. This article will introduce some precautions and common problems with MyBatis batch query statements, and provide specific code examples. Hope this can provide some help to developers. Things to note when using M

MySQL Jar package usage guide and precautions MySQL Jar package usage guide and precautions Mar 01, 2024 pm 04:21 PM

MySQL Jar package usage guide and precautions MySQL is a commonly used relational database management system. Many Java projects use MySQL as the backend for data storage. In a Java project, to interact with the MySQL database, you need to use the Java driver (i.e. Jar package) provided by MySQL. This article will introduce the usage guidelines and precautions for the MySQL Jar package, and provide specific code examples to help readers better use the MySQL driver. 1. M

What is Douyin multi-account management? What should I pay attention to when using multiple Douyin accounts? What is Douyin multi-account management? What should I pay attention to when using multiple Douyin accounts? Mar 21, 2024 pm 10:01 PM

As one of the most popular short video platforms today, Douyin has attracted a large number of users and businesses. In order to better operate and promote their Douyin accounts, many users choose to use multiple accounts. So, what is Douyin multi-account management? This article will answer this question for you and discuss what you need to pay attention to when managing multiple accounts on Douyin. 1. What is Douyin multi-account management? Douyin multi-account management refers to the simultaneous management and operation of multiple Douyin accounts by users or companies in order to better operate and promote their own Douyin accounts. These accounts can be personal accounts, corporate accounts, brand accounts, etc. Through multi-account management, users can better divide labor, coordinate operations, and improve the operational efficiency and effectiveness of their accounts. The advantages of Douyin’s multi-account management are as follows: 1. Division of work and coordination

What are the considerations for using C++ lambda expressions? What are the considerations for using C++ lambda expressions? Apr 17, 2024 pm 12:15 PM

Be careful when using C++ lambda expressions: capture variables carefully to avoid accidental modifications. Variables can be captured by reference or value, and reference capture is used to modify external variables. A lambda expression has a different lifecycle than the function that captures it, potentially causing memory leaks. Consider using function pointers or function objects to optimize performance.

See all articles