You can use the in
operator to check if a given index is present in the array, regardless of its actual value.
var t = []; t[0] = undefined; t[5] = "bar"; console.log( 0 in t ); // true console.log( 5 in t ); // true console.log( 1 in t ); // false console.log( 6 in t ); // false if( 0 in t && t[0] === undefined ) { // the value is defined as "undefined" } if( !(1 in t) ) { // the value is not defined at all }
pomeh source share