JavaScript has evolved dramatically in recent years, with big updates and features such as ES6 classes and async/await. It has also introduced powerful new operators and methods that often fly under the radar. These “hidden gems” include new operators such as the nullish coalescing and optional chaining operators for null checking and the spread, rest, and logical assignment operators for simplifying working with objects and arrays.
Optional Chaining
The optional chaining operator ( ?. ) helps to avoid the dreaded “cannot read property of undefined” error by providing access to deeply nested object properties without causing an error to be thrown if a property in the chain is undefined or null.
const user = { profile: { name: "Alice" } }; console.log(user?.profile?.name); // Output: "Alice" console.log(user?.settings?.theme); // Output: undefined
It also works when using dynamic property names (ex: settings.theme?.[“propertyName”] ). This operator is particularly useful when working with optional or unpredictable data and can even call methods safely. For example, if the method “greet” doesn’t exist, user.greet?.() won’t throw an error. While it is powerful, optional chaining can be overused, as it can mask potential issues in code. Debugging may become harder if things are silently bypassed; therefore, it is best used for truly optional or unpredictable data.
etc.