Richard Beattie

Optional Chaining in Javascript

This is just a a small javascript operator which is quite cool

Often I find myself having to write something like this

if (obj && obj.prop1 && obj.prop1.prop2 && obj.prop1.prop2.prop3) {
	// Type safe way of accessing prop3

With the optional chaining operator you can write this instead

if (obj?.prop1?.prop2?.prop3) {
	// Type safe way of accessing prop3

Which in my option is much more readable and just nicer to write.

The gist is that using this operator makes it safe to chain accessing properties where one might be undefined or null

You can see more about this at: