Home >Web Front-end >JS Tutorial >JavaScript Switch Statement_Basics

JavaScript Switch Statement_Basics

WBOY
WBOYOriginal
2016-05-16 19:15:451140browse

Conditional declarations in JavaScript are used to complete behaviors based on different conditions.
JavaScript Switch Statement
If you want to choose to execute one of several code blocks, you can use the switch statement:
Syntax:

Copy code The code is as follows:

switch(n)
{
case 1:
Execute code block 1
break
case 2:
Execute code block 2
break
default:
If n is neither 1 nor 2, execute this code
}

Working principle: behind switch (n) can be an expression or (and usually is) a variable. The value in the expression is then compared with the number in the case. If it matches a case, the following code will be executed. The function of break is to prevent the code from automatically executing to the next line.
Example:

[Ctrl A select all Note: If you need to introduce external Js, you need to refresh to execute
]
Statement:
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