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 best way to use the extension operator for null checking in JavaScript

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

Share

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

This article mainly introduces what is the best way to use the extension operator to check null in JavaScript. It is very detailed and has some reference value. If you are interested, you must finish it.

The best way to do null check

Do you remember the first empty check code you wrote? When JavaScript wasn't as perfect as it is now, I wrote this in my old code:

If (foo! = = null & & foo! = = undefined) {}

Later, my life was saved by a simple if!

If (foo) {}

As long as the conditional value foo is not the following values, it will be true. "

Null

Undefined

NaN

Empty string (")

False

In addition to simple if, the optional chain operator for modern JavaScript?. Merge operator with null value? Can make our code more concise.

1. Optional chain operator

The optional chain operator is a secure way to access properties of nested objects. This means that we don't have to do multiple null checks when accessing a long list of object properties. When trying to access object properties that may not exist, the optional chain operator will make the expression shorter and more concise

The following example checks whether the zip code of the customer address is null:

Const client = {name: 'Liu Xing', address: {zipcode:' 1234'}} / / the old value method if (client & & client.address & & client.address.zipcode) {} / / the more modern alternative chain operator method if (client?.address?.zipcode) {} 2. Null merge operator

The null merge operator is a logical operator that returns its right Operand when the left Operand is null or undefined, otherwise it returns the left Operand.

Const defaultMessage = 'Zen of Hello JavaScript' const msg = defaultMessage? 'Hello Liu Xing';console.log (msg); / / Zen of Hello JavaScript'

If defaultMessage is null or undefined, Hello Liu Xing will be printed out.

When we use it sequentially, it becomes more powerful:

Console.log (firstName?? LastName?? 'anonymous')

In this example, if firstName is not null/undefined, it will display firstName, otherwise, if lastName is not null/undefined, it will show lastName. Finally, if they are all null/undefined, it will display "anonymous".

This is all of the article "what are the best ways to use extension operators for null checking in JavaScript?" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to 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