You can use optional chaining (?.
) with an array by adding the ?.
symbol before the array index accessor, like so:
// ES11+ array?.[index]
For example:
// ES11+ const arr = ['foo', 'bar', 'baz']; console.log(arr?.[0]); // 'foo' console.log(arr?.[10]); // undefined
Hope you found this post useful. It was published . Please show your love and support by sharing this post.