Home > Web Front-end > Front-end Q&A > What does case in javascript mean?

What does case in javascript mean?

WBOY
Release: 2022-01-10 11:51:53
Original
4845 people have browsed it

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}".

What does case in javascript mean?

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 不同时执行的代码
}
Copy after login

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>
Copy after login

Output result:

What does case in javascript mean?

##[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!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template