Check for existence of key in multidimensional array in javascript

With the first three assignments your array actually looks like this:

a = [['1','2']]

Reading a[0][2] just returns undefined because a[0] exists but its property '0' is not defined.

But trying to read a[1][0] throws a TypeError because a[1] is already undefined and isn’t an object and thus doesn’t have any properties. This is also what your error message says:

Cannot read property '0' of undefined.

You can solve this problem by first checking for a[1] and then checking for a[1][0] using the typeof operator:

if (typeof a[1] !== 'undefined' && typeof a[1][0] !== 'undefined')

On supported platforms, We can just do:

if(a[3]?.[3]){
  // Do something
}

Check first if the first dimension exists then if the key in the second dimension exists

The logic will return false if the first test returns false, and tests the second dimension only if the first one is true.

  if(a[1] == undefined && a[1][2] == undefined)