Better Programming

Advice for programmers.

Follow publication

Member-only story

Optional Chaining, Null Coalescing and How They Will Change the Way You Write Code

Sam Sedighian
Better Programming
Published in
4 min readJan 15, 2020

--

Photo by Simon Abrams on Unsplash

If you have been following TypeScript releases recently you already know that Optional Chaining and Nullish Coalescing were introduced in TypeScript 3.7. They are both part upcoming features of the JavaScript and as of the time of publishing this piece, they are at stage 4 and stage 3 respectively.

UPDATED: As of Babel 7.8.0 These features are also included by default.

Optional Chaining

Quite often we find ourselves working deep into an object to get the value of a property. If you’ve written more than 100 lines of code in JavaScript you know that, when not done right, this can be the root of a common error: Uncaught TypeError.

Uncaught TypeError
A common issue is accessing values in JavaScript is hitting the dreaded Cannot read property of undefined

We are going to use this mock response for the rest of the examples:

Sample data response

--

--

Sam Sedighian
Sam Sedighian

Written by Sam Sedighian

Software Engineer @Doordash. Formerly @LinkedIn, @Tubi

Write a response