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 if,else,elseif,switch of JavaScript

2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the relevant knowledge of how to use JavaScript's if,else,elseif,switch, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this article on how to use JavaScript's if,else,elseif,switch. Let's take a look at it.

Conditional statements are used to perform different actions based on different conditions.

Conditional statement

When you write code, you often need to perform different actions based on different judgments.

You can do this by using conditional statements in your code.

In JavaScript, we can use the following conditional statement:

Use if to specify the block of code to execute, if the specified condition is true

Use else to specify the block of code to execute, if the same condition is false

Use else if to specify the new condition to test, if the first condition is false

Use switch to specify multiple alternative blocks of code to be executed

If statement

Use the if statement to specify the block of JavaScript code to be executed if the condition is true.

Grammar

If (condition) {

Code executed if the condition is true

}

Note: if uses lowercase letters. Uppercase letters (IF or If) produce JavaScript errors.

Example

If the time is earlier than 18:00, send a "Good day" greeting:

If (hour < 18) {

Greeting = "Good day"

}

If the time is earlier than 18:00, the result of greeting will be:

Good day

Else statement

Use the else statement to specify the block of code if the condition is false.

If (condition) {

Block of code executed when the condition is true

} else {

Block of code executed when the condition is false

}

Example

If hour is less than 18, create a "Good day" greeting, otherwise "Good evening":

If (hour < 18) {

Greeting = "Good day"

} else {

Greeting = "Good evening"

}

Results of greeting:

Good day

Else if statement

Use else if to specify the new condition when the first condition is false.

Grammar

If (condition 1) {

Block of code executed when condition 1 is true

} else if (condition 2) {

Block of code executed when condition 1 is false and condition 2 is true

} else {

Block of code executed when condition 1 and condition 2 are both false

}

Example

If the time is earlier than 10:00, create a "Good morning" greeting, if not, but the time is earlier than 18:00, create a "Good day" greeting, otherwise create a "Good evening":

If (time < 10) {

Greeting = "Good morning"

} else if (time < 18) {

Greeting = "Good day"

} else {

Greeting = "Good evening"

}

Results of greeting:

Good day

This is the end of the article on "how to use JavaScript's if,else,elseif,switch". Thank you for reading! I believe you all have a certain understanding of "how to use JavaScript's if,else,elseif,switch". If you want to learn more, you are welcome to follow the industry information channel.

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: 229

*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