JSFiddle - React, Tailwind, and code Playground

by Drath

HTML

Checking multidimensional arrays for undefined.

JavaScript

var test = [];
test[1] = [];
test[1][1] = "test";

document.write(test[1][1]);

//document.write(test[2][1]); 
//returns TypeError: Cannot read property '1' of undefined 

/*
if(test[2][1]) {
   document.write(test[2][1]);
}
if(typeof test[2][1] != 'undefined') {
   document.write(test[2][1]);
}
*/

//All return the same error

//You need check the first array, first to not throw the error
//This is because you are checking if an object exists on an object that doesn't exist, so it will return true?

if(test[2]) {
   if(test[2][1]) {
       document.write(test[2][1]);
   }
}

//Let's see if it works
test[2] = [];
test[2][1] = 'test';

if(test[2]) {
   if(test[2][1]) {
       document.write(test[2][1]);
   }
}