What is the difference between the undefined
elements in JavaScript arrays:
- created directly,
- created with elision?
Elements of case 1. and 2. both have undefined
types and values, but the ones born in elision don't have indexes (or any enumerable properties, their array has a correct Array.length
that's clear). Can you give some background on this?
Example:
// case 1.
const undefineds = [undefined, undefined, undefined].map((el, i) => i)
console.log(undefineds, undefineds.length, 'undefineds')
// case 2.
const empties = [,,,].map((el, i) => i)
console.log(empties, empties.length, 'empties')
Output:
Array [0, 1, 2] 3 "undefineds"
Array [undefined, undefined, undefined] 3 "empties"
Note: In case 2. I am aware of not just the indexes i
would be undefined, but anything returned from the iteration, e.g.: [,,,].map((el, i) => 'puppies')
=> [undefined, undefined, undefined]
.