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].
mapover an array with elisions, the point is that the callback doesn't get called at all on the non-existing elements. It's not like it's called withundefinedfor the value andundefinedfor the index. Also, it does not return an array ofundefineds, it does return another sparse array - yourconsole.logimplementation appears to be confused by that though. It should log[,,,]or[empty, empty, empty]or something like that.Array.map's docs it seems.