In JavaScript, case means judgment condition. It is often used in conjunction with the switch statement to perform different actions based on different conditions. The syntax is "switch(n){case 1: execute code block 1 break ; default: code that is not executed at the same time as case1}".
The operating environment of this tutorial: Windows 10 system, JavaScript version 1.8.5, Dell G3 computer.
What is the meaning of case in javascript
The switch statement is used to perform different actions based on different conditions.
JavaScript switch statement
Use the switch statement to select one of multiple blocks of code to execute.
Syntax
switch(n) { case 1: 执行代码块 1 break; case 2: 执行代码块 2 break; default: 与 case 1 和 case 2 不同时执行的代码 }
How it works: First set the expression n (usually a variable). The value of the expression is then compared to the value of each case in the structure. If there is a match, the code block associated with the case is executed. Please use break to prevent the code from automatically running to the next case.
The example is as follows:
<html> <body> <p id="demo"></p> <script> var day; switch (new Date().getDay()) { case 0: day = "周日"; break; case 1: day = "周一"; break; case 2: day = "周二"; break; case 3: day = "周三"; break; case 4: day = "周四"; break; case 5: day = "周五"; break; case 6: day = "周六"; } document.getElementById("demo").innerHTML = "今天是" + day; </script> </body> </html>
Output result:
##[Related recommendations:javascript learning tutorial 】
The above is the detailed content of What does case in javascript mean?. For more information, please follow other related articles on the PHP Chinese website!