[Javascript] Safely Access a Property on a JavaScript Array with Optional Chaining

let other = null
console.log(other?.[0]) // undefined

You can use optional chaining to access elements in an array. If the array is null or undefined, attempting to access an element with optional chaining prevents an error from being thrown, returning undefined instead.

posted @ 2021-11-03 01:05  Zhentiw  阅读(24)  评论(0编辑  收藏  举报