Home / Companies / LogRocket / Blog / Post Details
Content Deep Dive

Optional chaining and nullish coalescing in JavaScript

Blog post from LogRocket

Post Details
Company
Date Published
Author
Danny Guo
Word Count
1,010
Language
-
Hacker News Points
-
Summary

Optional chaining and nullish coalescing are two new JavaScript operators designed to improve code readability and safety by simplifying how developers handle undefined values and default settings. Optional chaining allows for succinctly accessing nested object properties without having to repeatedly check for undefined values, thereby avoiding exceptions like the TypeError. This operator is akin to the safe navigation operator found in other programming languages and can be used with both dot and bracket notation as well as with function calls. Nullish coalescing, on the other hand, offers a cleaner way to assign default values by only rejecting null or undefined, unlike the logical OR operator, which can mistakenly override falsy but valid values like 0. Although these operators are not yet part of the formal ECMAScript specification, they have reached stage 3 in the TC39 process and are supported by TypeScript, Babel, and Prettier, reflecting the JavaScript community's enthusiasm for their adoption. These advancements promise to make JavaScript code both safer and more concise while facilitating better error handling and debugging practices through tools like LogRocket, which can capture detailed user interactions and error contexts.