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 understand the javascript ternary operator

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

Share

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

This article mainly introduces "how to understand javascript trinomial operator". In daily operation, I believe many people have doubts about how to understand javascript trinomial operator. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to understand javascript ternary operator". Next, please follow the editor to study!

In javascript, the ternary operator does not have an equal sign, it consists of a question mark and a colon, the syntax format "conditional expression? expression 1: expression 2;"; if the result of the conditional expression is true (true), the code in expression 1 is executed, otherwise the code in expression 2 is executed.

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

Ternary operators in javascript

Ternary operators (also known as ternary operators, conditional operators), consisting of a question mark and a colon, have the following syntax format:

Conditional expression? Expression 1: expression 2

The conditional expression must be a Boolean expression, while expression 1 and expression 2 are values of any type.

If the result of the conditional expression is true, the code in expression 1 is executed and the value of the expression is returned.

If the result of the conditional expression is false, the code in expression 2 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.

At this point, the study on "how to understand the javascript trinomial operator" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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