JavaScript optional link

Learn about upcoming new JavaScript features

ThisOptional link operatorIt is a new feature in the next ECMAScript standard.

It's still not official, butavailable in Chrome 80Behind the sign.

Have you ever used the && operator as a fallback? This is one of my favorite JavaScript features.

In JavaScript, you can first check whether the object exists, and then try to get one of its properties, as follows:

const car = null
const color = car && car.color

even ifcarIs empty, you have no errors, andcolorWas assignednullvalue.

You can divide into multiple levels:

const car = {}
const colorName = car && car.color &&

In some other languages, use&&Since it is usually a logical operator, it may give you a true or false judgment.

Not in JavaScript, it allows us to do some cool things.

Now, this new optional chaining operator will make us like it even more:

const color = car?.color
const colorName = car?.color?.name

in casecarYesnullorundefined, The result will beundefined.

There is no error (when using &&,carused to beundefinedWe have aReferenceError: car is not definederror)

You can use the following syntax todayThis Babel plugin.

Download mine for freeJavaScript beginner's manual

More js tutorials: