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

What are the three conditional statements of JavaScript

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

Share

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

This article introduces the relevant knowledge of "what are the three conditional statements of JavaScript". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Three kinds of conditional statements of JavaScript: 1, "if else" statement, syntax "if (conditional) {...} else {...}"; 2, "switch...case" statement; 3, ternary operation statement, syntax "conditional expression? expression 1: expression 2;"

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

Conditional judgment sentence is a frequently used form of sentence in the process of program development. Like most programming languages, conditional judgment statement is also available in JavaScript. The so-called conditional judgment means that the program performs different operations according to different conditions, such as displaying different contents according to age, and judging whether the operation is successful or failed according to the Boolean value true or false.

I. if-else statement

1. Grammar

There are three grammars for if-else:

(1) if statement

If (condition) {execute code when condition is true}

(2) if else statement

If (condition) {code executed when condition is true} else {code executed when condition is false}

(3) if else if else statement

If (condition 1) {Code executed when condition 1 is true} esle if (condition 2) {condition 1false condition 2true} else {all false}

Examples

IfElse var myage = 10; / / first grammar if (myage > 5) {[xss_clean] ln ("you are older than 5 years old");} / / second grammar if (myage > 15) {[xss_clean] ln ("you are older than 15 years old");} else {[xss_clean] ln ("your age is less than 15 years old") } / / third grammar if (myage > 5) {[xss_clean] ln ("you are older than 5");} else if (myage)

< 15 ){ [xss_clean]ln("你的年龄大于15小于5岁"); } else { [xss_clean]ln("你的年龄大于等于15岁") } 输出结果为

II. Switch...case statement

1. Grammar

Switch (expression n) {case 1: execution block 1; break;case 2: execution block 2; break;default: code that is not executed at the same time as case 1 and case 2}

2. Working principle

First set the expression n (usually a variable). The value of the expression is then compared to the value of each case in the structure. If there is a match, the block of code associated with the case is executed. Use break to prevent the code from automatically running to the next case.

Examples

Switch var flag = 2; switch (flag) {case 1: [XSS _ clean] ("disturbing the world, all understanding.") ; break; case 2: [XSS _ clean] ("just let me stay with you.") ; break; default:break;}

The output is

Ternary operator

The ternary operator (also known as the conditional operator), which consists of a question mark and a colon, has the following syntax format:

B? X: y

The b Operand must be a Boolean expression, and x and y are values of any type.

If the return value of Operand b is true, the x Operand is executed and the value of the expression is returned.

If the return value of Operand b is false, the y Operand is executed and the value of the expression is returned.

Example:

Define the variable a, and then detect whether an is assigned, and use it if assigned; otherwise, set the default value.

Var a = null; / / defines the variable atypeof a! = "undefined"? A = a: a = 0; / / detect whether the variable an is assigned, otherwise set the default value console.log (a); / / display the value of the variable an and return null

Conditional operators can be converted to conditional structures:

If (typeof a! = "undefined") {/ / assignment a = a;} else {/ / No assignment a = 0;} console.log (a)

It can also be converted to a logical expression:

(typeof a! = "undefined") & & (a = a) | | (a = 0); / / logical expression console.log (a)

In the above expression, if an is assigned, the (a = a) expression is executed, and the (a = 0) expression after the logic or operator is no longer executed; if an is not assigned, the (a = a) expression after the logic or operator is no longer executed, instead the expression after the logic or operator (a = 0) is executed.

Note:

The interference of false value needs to be considered in actual combat. Using typeof a! = "undefined" for detection can avoid being mistaken for no assignment when variables are assigned false values such as false, null, "", NaN, etc.

This is the end of the content of "what are the three conditional statements of JavaScript". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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