Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Switch, break and default of JavaScript

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use JavaScript's Switch, break and default". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use JavaScript's Switch, break and default".

Break keyword

If JavaScript encounters a break keyword, it jumps out of the switch code block.

This will stop the execution of more code in the code block and case testing.

If a match is found and the task is completed, execution is randomly interrupted (break). No more tests are needed.

Break can save a lot of execution time because it "ignores" the execution of other code in the switch code block.

You don't have to interrupt the last case in the switch code block. The code block ends naturally here.

Default keyword

The default keyword specifies the code to run when there is no case match:

Example

The getDay () method returns the week name of the number 0 through 6.

If today is neither Saturday (6) nor Sunday (0), output a default message:

Switch (new Date (). GetDay () {

Case 6:

Text = "Today is Saturday"

Break

Case 0:

Text = "Today is Sunday"

Break

Default:

Text = "looking forward to the weekend -"

}

The result of text is:

Looking forward to the weekend--

The default case does not have to be the last case in the switch code block:

Example

Switch (new Date (). GetDay () {

Default:

Text = "looking forward to the weekend!"

Break

Case 6:

Text = "Today is Saturday"

Break

Case 0:

Text = "Today is Sunday"

}

If default is not the last case in the switch code block, remember to end the default case with break.

Common code blocks

Sometimes you need different case to use the same code.

In this example, case 4 and 5 share the same code block, while 0 and 6 share another code block:

Example

Switch (new Date (). GetDay () {

Case 4:

Case 5:

Text = "the weekend is coming:)"

Break

Case 0:

Case 6:

Text = "Today is the weekend -"

Break

Default:

Text = "looking forward to the weekend!"

}

Thank you for your reading, the above is the content of "how to use JavaScript's Switch, break and default". After the study of this article, I believe you have a deeper understanding of how to use JavaScript's Switch, break and default, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report