How to Use JavaScript Optional Chaining With Array?

You can use optional chaining (?.) with an array by adding the ?. symbol before the array index accessor, like so:

// ES11+

For example:

// ES11+
const arr = ['foo', 'bar', 'baz'];

console.log(arr?.[0]); // 'foo'
console.log(arr?.[10]); // undefined

This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.