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

Detailed explanation of how to use switch statement in js

王林
王林forward
2020-03-25 10:55:354778browse

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:
    默认代码块
}

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

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!

Statement:
This article is reproduced at:jb51.net. If there is any infringement, please contact admin@php.cn delete