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 statements in javascript

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

Share

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

Most people do not understand the knowledge points of this article "how to use if sentences in javascript", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use if sentences in javascript" article.

In javascript, if statements are used to perform different actions according to different conditions, the syntax "if (conditional expression) {/ / code to be executed;}" or "if (conditional expression) {/ / code to be executed when the expression is valid} else {/ / code to be executed when the expression is not valid.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

In javascript, if statements are used to perform different operations according to different conditions.

There are three different forms of if statements:

If statement

If else statement

If else if else statement

If statement

The if statement is the simplest conditional judgment statement in JavaScript. The syntax format is as follows:

If (conditional expression) {/ / Code to be executed;}

When the conditional expression holds, that is, the result is a Boolean value of true, the code in {} is executed.

The sample code is as follows:

JavaScript var age = 20; if (age > = 18) {/ / if the result of age > = 18 is true, execute the code alert ("adult") in {} below;}

The running result is shown in the following figure:

If else statement

The if else statement is an upgraded version of the if statement, which specifies not only the code to be executed when the expression is established, but also the code to be executed when the expression is not valid. The syntax format is as follows:

If (conditional expression) {/ / Code to be executed when an expression is valid} else {/ / Code to be executed when an expression is not valid

The sample code is as follows:

JavaScript var now = new Date (); / / get the current full date var dayOfWeek = now.getDay (); / / get a number between 0 and 6 to indicate what day it is, 0 for Sunday, 1 for Monday, and so on if (dayOfWeek > 0 & & dayOfWeek)

< 6) { // 判断:如果当前是星期一到星期五中的一天,则输出"Have a nice day!",若不是则输出"Have a nice weekend!" alert("Have a nice day!"); } else { alert("Have a nice weekend!"); } 运行结果如下图所示:

If else if else statement

Both if and if else statements have only one conditional expression, while the if else if else statement is their more advanced form, which allows you to define multiple conditional expressions in the if else if else statement and execute the corresponding code based on the result of the expression. The syntax format is as follows:

Code executed if if (conditional expression 1) {/ / conditional expression 1 is true} else if (conditional expression 2) {/ / code executed when conditional expression 2 is true. Code to execute if else if (conditional expression N) {/ / conditional expression N is true} else {/ / code to be executed if all conditional expressions are false

Tip: during the execution of the if else if else statement, when it encounters a valid conditional expression, it will immediately execute the code in the following {}, and then exit the entire if else if else statement. If there is a valid conditional expression in the subsequent code, it will not be executed.

The sample code is as follows:

JavaScript var now = new Date (); / / get the current full date var dayOfWeek = now.getDay () / / get a number between 0 and 6 to indicate what day it is 0 means Sunday, 1 means Monday, And so on if (dayOfWeek = = 0) {/ / determine what day it is currently alert ("Sunday")} else if (dayOfWeek = = 1) {alert ("Monday")} else if (dayOfWeek = = 2) {alert ("Tuesday")} else if (dayOfWeek = = 3) { Alert ("Wednesday")} else if (dayOfWeek = = 4) {alert ("Thursday")} else if (dayOfWeek = = 5) {alert ("Friday")} else {alert (Saturday)}

The running result is shown in the following figure:

Matters needing attention

When using nested if else, if you have only one line of statements, you should also wrap them in curly braces to avoid conditional ambiguity.

For example, the following nesting if else can easily lead to misunderstanding:

If (0) if (1) console.log (1); else console.log (0)

For the above code, the JavaScript interpreter will interpret it according to the nearest principle and at the following logical level:

If (0) if (1) console.log (1); else console.log (0)

So using curly braces can avoid a lot of problems:

If (0) {if (1) console.log (1);} else {console.log (0);} the above is about "how to use if sentences in javascript". I believe you all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please 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: 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