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

Do you need to add a semicolon to if in javascript?

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "do you need to add a semicolon to the if in javascript?" interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "does the if in javascript need to add a semicolon?"

The if in javascript does not add a semicolon, because the if statement is used to specify that if the condition is true, the syntax of the JavaScript code executed, such as "if (...) {...}", does not need a semicolon.

This article operating environment: windows7 system, javascript1.8.5 version, Dell G3 computer.

Do you add semicolons to if in javascript?

If statements in javascript do not have semicolons.

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 to execute 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) {code block executed when condition is true} else {code block executed when 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) {code block executed when condition 1 is true} else if (condition 2) {code block executed when condition 1 is false and condition 2 is true} else {code block 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 so far, I believe you have a deeper understanding of "do you need to add a semicolon to the if in javascript?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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