Optional chaining on a multidimensional array

Let's suppose that you define the following multidimensional array:

javascript
let siblings = [['Erik', 1990], ['Andrea', 1993], ['Paula', 2005]];

If you want to access to a specific index:

javascript
let name = siblings[2][0]; // Paula

But what if you try to access to a non-existent position:

javascript
let name = siblings[3][2]; //Uncaught TypeError: Cannot read property '2' of undefined

You can use optional chaining in multidimensional arrays to avoid the previous error:

javascript
let name = siblings[3]?.[2]; // undefined

Remember that optional chaining operator (?) should be followed by a (.) dot.

Hi, I'm Erik, an engineer from Barcelona. If you like the post or have any comments, say hi.