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 is the usage and meaning of.,? = in JavaScript.

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

Share

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

Today, I would like to share with you the relevant knowledge of the usage and meaning of JavaScript. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

Optional chain (.?)

If a value is null, or undefined. So what happens if we use some operators to call a method or access a property?

Let aitlet b = a.name

If it's the above code, then we can get an error.

In fact, this situation is equivalent to directly accessing name properties on undefined. Undefined and null are two special data types, which cannot be accessed by point operators. So when a variable may be null or undefined, and I happen to need to access a property of this variable, then we should do this.

Let a let bscape if (!! a) {b = a.name;} else {b = name}

Only when an exists, I access the name property of a. If you want to process it further, you can continue to determine the data type of the following a. However, this is not our focus today, so we will not talk about it any more. We can see that with such a simple logic, we have to write so much, so is there a simple way to write it? Look at the following example

Let aitlet b = a?.name

We finally see.? This thing, in fact, this is called optional chain, and the meaning expressed is the same as the example of if else just now. Only when an exists and a has the name attribute will the value be assigned to b, otherwise the undefined will be assigned to b. The important thing is that there will be no error in doing so, whether an exists or not.

Of course, we can still do it.

Let aittlet bitterb = axi.nameplate. Age.hast.haploid. Is not to report an error a?.b?.c ("still do not report an error")

We can see that you can follow the queue indefinitely, no matter how many attributes there are, as long as there is a final access to the attribute, access to the final result, it will be assigned to b, otherwise, undefined will be assigned to b. (only in this way can we call it chain structure.)

Null merging operator (?)

With the above example, let's keep it simple and go straight to the example.

Let bsetlet a = 0 let c = {name:'buzhimingqianduan'} if (!! a | | a = 0) {b = a;} else {b = c;}

Yes, that is the above example, when we want to judge that a value exists, but it is equal to 0, we also need to regard it as it exists, so we have the above example, in fact, we can still do this.

Let bantlet a = 0 Bitlet c = {name:'buzhimingqianduan'} b = a?? C

In the above example, when any value of an except undefined or null, b equals a, otherwise it equals c.

Null assignment operator (? =)

Similar to the example above

Let b = 'Hello'; let a = 0let c = null;let d = '123' b? = a; / b = "Hello" c? = d / / c = '123'

The value of the variable on the right will be assigned to the variable on the left when the value on the left side is null or undefined. All other values are not assigned. Also in some scenarios, a lot of code can be omitted.

Interesting question and answer time: let a-share let b = "unknown front end" let c = null;let d = 0 * let e?? = axi.b? C?? D?.a?? B. console.log (e)

What is the result?

Haha, a bunch of question marks.

It is worth noting that:? It is a wrong value that ignores null, undefined, etc.

Var ibo = {} console.log (ibo?.a? 111l) / / 111var ibo = {a: {BGV 1}} console.log (ibo?.a?.b? 111l) / / 1console.log (1x) / / 1console.log (null?? "xx") / / xxundefinedconsole.log (undefined? "xx") / / xxconsole.log ('?? "xx") / / the null values above are all the contents of the article "what is the usage and meaning of." in JavaScript. Thank you for your reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to 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