JavaScript switch statement

JavaScript switch

JavaScript switch allows selection of multiple possible results of a scalar (expression).

Syntax:

switch (expr) {
case result1:
statement1
break;
case result2:
statement2
break;
……
default:
statement
}

Syntax explanation

The system calculates expr Value, select and execute the corresponding statement below based on the calculation results (result1, result2, etc.). If all case results do not match, the statement in default will be executed.

break is used to jump out of the process after executing the code. Although it can be omitted grammatically, do not omit it except in special circumstances. Otherwise, the following code will continue to be executed, even if the calculated expr result does not match the case (this is the same as if else difference).

    php中文网(php.cn)  

Tips

There can be multiple case conditional judgments

case The subsequent results are not limited to numbers, but also Is a character or other type supported by JavaScript

default keyword

Please use the default keyword to specify what to do when the match does not exist:

   php中文网(php.cn) 


Continuing Learning
||
php中文网(php.cn)
submit Reset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!