Optional chaining helps to avoid "undefined is not a function" exceptions

This post is part of my Today I learned series in which I share all my learnings regarding web development.

I was reading the MDN docs for optional chaining and came across a fact I didn't know about it.

Before we dive in, let's have a look at the new optional chaining feature very quickly.

This new JavaScript language addition allows developers to access optional values that are nested deeply inside of an object in a safer way.

The process of accessing a deeply nested object property can be very tedious. It could be that the object does not have the structure you expect or that it doesn't define the values you're looking for. To avoid thrown exceptions developers had to check every single property for its existence before accessing the deeply nested property.

The new ?. syntax helps developers to access properties in a safe manner even if the object structure is different than expected. Let's have a look at an example:

const someObject = { foo : { bar : { baz : 'someValue' } } } ; if ( someObject . foo && someObject . foo . bar && someObject . foo . bar . baz ) { console . log ( someObject . foo . bar . baz ) ; } else { console . log ( 'noValue' ) ; } console . log ( someObject . foo ?. bar ?. baz || 'noValue' ) ; console . log ( someObject . foo ?. bar ?. doesNotExist || 'noValue' ) ; console . log ( someObject . doesNotExist ?. foo ?. bar || 'noValue' ) ;

The optional chaining feature proposal is currently on stage 4 of the ECMAscript proposal process which means it will be part of the EcmaScript 2020 additions. Chrome already implements it behind the feature flag "Experimental JavaScript".

# Execute methods if they exist and return undefined otherwise

What I didn't know was that this proposal also includes a mechanism to execute object methods that potentially are undefined using ?.() .

const someObject = { foo ( ) { return 'someValue' ; } } ; console . log ( someObject . foo ?. ( ) || 'notDefined' ) ; console . log ( someObject . bar ?. ( ) || 'notDefined' ) ;

In my opinion, optional chaining and its optional function execution is a very welcome language addition, which will help to avoid a few undefined is not a function exceptions.

That said, exception are there for a reason and developers should be very careful with these new features that make exceptions "disappear". If optional chaining is used very often and/or only used to hide exceptions, it's probably a hint to rethink the overall application architecture.

I can't wait for optional chaining to be supported across browsers. If you want to use it today, babel has you covered. And with that – have fun, friends! 👋

Edited: As TheIncorrigible1 pointed out on Reddit optional chaining works perfectly together with Nullish Coalescing which you might want to check out, too.