JavaScriptIntermediate#es6#operators

What is optional chaining and nullish coalescing?

?. short-circuits to undefined instead of throwing when a link in the chain is null or undefined. ?? returns the right side only when the left is null or undefined, unlike || which also replaces 0 and ''.

Example
user?.address?.city;   // undefined, no crash
const port = cfg.port ?? 3000; // keeps 0 if given
const bad = cfg.port || 3000;  // 0 becomes 3000

Related Questions

1
JavaScriptBeginner#fundamentals

Difference between null and undefined?

Open
2
JavaScriptIntermediate#es6#collections

Difference between Map/Set and plain objects/arrays?

Open
3
JavaScriptIntermediate#objects

What is the difference between shallow copy and deep copy?

Open