Home > Web Front-end > JS Tutorial > body text

Detailed explanation of JavaScript's null value coalescing operator (??)

青灯夜游
Release: 2020-12-02 18:02:51
forward
3392 people have browsed it

This article will introduce you to JavaScript’s null value merging operator (??). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

Detailed explanation of JavaScript's null value coalescing operator (??)

This year ECMAScript 2020 (ES2020) will be released! Since the release of ECMAScript 2015 (ES6) in 2015, we have updated the JavaScript language every year.

Directory

  • Using JavaScript null value coalescing operator
  • Usage example
  • null Value combining operator with logical OR (||)
  • Browser support
  • Summary

In ES2020, we got Features that have long been available in other languages ​​such as C# and PHP: Null Coalescing Operator. I've always liked this name because I feel smart every time I say it.

The null value coalescing operator will traverse the list and return the first value that is not null or undefined.

It is important to note that the null coalescing operator only looks for null or null values. The null coalescing operator accepts falsy values ​​(Falsy values).

Using the JavaScript null coalescing operator (??)

Let’s look at some examples. Remember that JavaScript's null coalescing operator will follow a ?? chain until a non-null or undefined object is found. If false is found, it will return that value.

null      ?? 'hi'       // 'hi'
undefined ?? 'hey'      // 'hey'

false     ?? 'hola'     // false
0         ?? 'bonjour'  // 0
'first'   ?? 'second'   // first
Copy after login

In the following example, we store some values ​​in a variable:

let person  // <-- person is undefined here

person ?? { name: &#39;chris&#39; }       // { name: &#39;chris&#39; }

const isActive = false

isActive ?? true             // false
Copy after login

Linking JavaScript’s null value coalescing operator

The great thing about JavaScript’s null coalescing operator is that we can chain it as many times as we need.

null ?? undefined ?? false ?? &#39;hello&#39;     // false
null ?? &#39;&#39; ?? &#39;hello&#39;                     // &#39;&#39;
Copy after login

Usage Example

Can be used when getting data from external sources. For example, we want to crawl blog articles from multiple places. You can then determine which post will become our featured post:

// 简化代码。 使用 fetch requires 需要比这更多的代码
const firstBlogPost = await fetch(&#39;...&#39;)
const secondBlogPost = await fetch(&#39;...&#39;)
const defaultBlogPost = { title: &#39;Default Featured Post&#39; }

const featuredBlogPost = firstBlogPost ?? secondBlogPost ?? defaultBlogPost
Copy after login

The above is a good way to set default values ​​if you're not sure whether certain values ​​exist.

Empty coalescing operator and logical OR (<span style="font-size: 18px;">||</span>)

If you want to eliminate For virtual values, you can use the logical OR operator (||).

Essentially, it does the same thing as the null coalescing operator, except that it eliminates imaginary values.
  • The null value coalescing operator will skip null, undefined
  • The logical OR operator will skip null, undefined, false
false ?? &#39;hello&#39;    // false
false || &#39;hello&#39;    // &#39;hola&#39;
Copy after login

If you don’t want to use virtual values, you can use ||. If you just want to check if it is null or undefined, use ??.

Browser Support

As of this writing, the null coalescing operator is available in the latest versions of Chrome, Firefox, Edge, and Safari.

Detailed explanation of JavaScripts null value coalescing operator (??)

Summary

The null coalescing operator is a nice addition to the JavaScript language. It doesn't hurt to have options with more check values.

English original address: https://scotch.io/tutorials/javascripts-null-coalescing-operator

Author: Chris on Code

Translation address: https://segmentfault.com/a/1190000023577464

For more programming-related knowledge, please visit: Introduction to Programming! !

The above is the detailed content of Detailed explanation of JavaScript's null value coalescing operator (??). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template