Home > Web Front-end > JS Tutorial > body text

Can case write two values ​​in js?

下次还敢
Release: 2024-05-06 14:09:14
Original
936 people have browsed it

Yes, case statements in JavaScript can contain multiple values, allowing checking for multiple similar values ​​or contiguous ranges of values ​​in a single branch.

Can case write two values ​​in js?

Can a case statement in JavaScript contain multiple values?

Answer: Yes

Details:

JavaScript case statements allow inclusion in a single case branch Multiple values. This syntax can be used in the following situations:

  • When multiple similar values ​​need to be checked.
  • When the value belongs to a continuous range.

Syntax:

<code class="javascript">switch (expression) {
  case value1:
  case value2:
    // 执行代码
    break;

  default:
    // 处理其他值
}</code>
Copy after login

Example:

Check whether the value of a numeric variable is between 1 and 5 Interval:

<code class="javascript">switch (number) {
  case 1:
  case 2:
  case 3:
  case 4:
  case 5:
    console.log("数字在 1 到 5 之间");
    break;

  default:
    console.log("数字不在 1 到 5 之间");
}</code>
Copy after login

Note:

  • The value in each case branch must be unique and clear.
  • If the values ​​do not match, the code in the default branch is executed.
  • The break statement (optional) is used to break out of the switch statement.

The above is the detailed content of Can case write two values ​​in js?. For more information, please follow other related articles on the PHP Chinese website!

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