JavaScript empty merge

Introduction to JavaScript's new features: empty merge

A new JavaScript feature that will be widely used isEmpty mergeoperator??.

what is that????

Have you used||Set the default value, if the variable is null or undefined?

For example, like this:

const myColor = color || 'red'

Well, invalid merge will replace||over there:

const myColor = color ?? 'red'


Well, when used, there are various errors hidden under the surface||Provide a fallback value.

in short,||Treat the value as false.??Treat the value as a null value (hence the name).

this means||If the first operand is, then the second operand is calculatedundefined,null,false,0,NaNor''.

??On the other hand restrict this list to onlyundefinedwithnull.

Which might be more suitable for your use case!

Thisv8.devThere is a more detailed article about this operator on the blog, but your main point is here.

Currently available in Chrome Canary andTower of Babel!

Check mineTongtian Tower Guide

Download mine for freeJavaScript beginner's manual

More js tutorials: