Javascript Tip — Use Optional Chaining

xameeramir
1 min readSep 29, 2020

Sometimes we need to check the existence/truthiness of properties on objects/classes. For example, this is how we generally write it:

if (family && family.mother && family.mother.child){
console.log(family.mother.child);
}

This can be written in short like this:

console.log(family?.mother?.child);

The magical operator being used here i.e. ?. is called the Optional chaining operator.

Read more: https://blog.nordible.com/Javascript-Tip-Use-Optional-Chaining/

--

--