Let's suppose that you define the following multidimensional array:
javascriptlet siblings = [['Erik', 1990], ['Andrea', 1993], ['Paula', 2005]];
If you want to access to a specific index:
javascriptlet name = siblings[2][0]; // Paula
But what if you try to access to a non-existent position:
javascriptlet 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:
javascriptlet 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.