Home > Web Front-end > JS Tutorial > Detailed explanation of how to use switch statement in js

Detailed explanation of how to use switch statement in js

王林
Release: 2020-03-25 10:55:35
forward
4987 people have browsed it

Detailed explanation of how to use switch statement in js

The switch statement is used to perform different actions based on different conditions.

The syntax format is as follows:

switch(表达式) {
   case n:
    代码块
    break;
   case n:
    代码块
    break;
   default:
    默认代码块
}
Copy after login

Code explanation:

Calculate the switch expression once;

Compare the value of the expression with the value of each case Compare;

If there is a match, execute the associated code.

is as follows:

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 = "星期六";
}
Copy after login

Recommended related tutorials: js tutorial

The above is the detailed content of Detailed explanation of how to use switch statement in js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
Latest Issues
Where is js written?
From 1970-01-01 08:00:00
0
0
0
js addClass not working
From 1970-01-01 08:00:00
0
0
0
js file code not found
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template