What are all the methods that can do that?
I only know of two methods. To summarize, to find whether arr[1]
is empty or undefined, use
1 in arr
or
Object.keys(arr).includes("1")
but the second method may possibly create a big array to begin with.
// running inside of Node 6.11.0
// Methods that works:
> a = Array(3)
[ , , ]
> b = Array.from({length:3})
[ undefined, undefined, undefined ]
> 1 in a
false
> 1 in b
true
> Object.keys(a).includes("1")
false
> Object.keys(b).includes("1")
true
// Method that doesn't
> a[1]
undefined
> b[1]
undefined
> a[1] === undefined
true
> b[1] === undefined
true