Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use if,else,elseif,switch of JavaScript

Shulou Source: shulou.com Published: 2022-06-01 05:38:51 09月10日 Update

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.

Tags: Conditions codes statements differences time greetings examples knowledge results content actions letters articles grammar same value multiple uppercase uppercase letters at the same time Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Apple Docker Shulou Tech Info Microsoft