Javascript Tip - Use Optional Chaining

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 Optional chaining operator. Use it to shortify your code base.

Read more: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining



Nordible is a digital solutions provider helping organizations achieve their digital goals.

We develop and measure digital solutions, implement impactful brand promotion and SEO strategies and develop software driven by rigorous testing that help your business succeed online and beyond.

You can contact us at info@nordible.com for business inquiries.

Follow us: