Skip to content
Astera Technology

Distinguish an empty array slot from an undefined element

Inspect index presence before counting map callbacks in a sparse array.

Download complete case notes and answer keys ↓

Trace the fixed program

The array [1,,undefined] has length 3. Compare its omitted middle slot with its explicit final undefined and record every map callback.

Complete original program
const values=[1,,undefined];const visited=[];
values.map((value,index)=>{visited.push({index,type:typeof value});return value;});
console.log(JSON.stringify({length:values.length,hasMiddle:1 in values,hasLast:2 in values,visited}));
Exact console output
{"length":3,"hasMiddle":false,"hasLast":true,"visited":[{"index":0,"type":"number"},{"index":2,"type":"undefined"}]}
Values and execution
IndexProperty presentCallback observation
0truenumber
1falseSkipped
2trueundefined

Explain the result

map checks whether each index property exists, including inherited properties, before calling the callback. This fresh ordinary array has no inherited numeric entries: index 1 is absent and index 2 is present. JSON can render both holes and undefined array entries as null, so it is not a presence test.

Test the alternative

The same JSON text can conceal different property presence. A hole and an explicit undefined both serialize here as null.

Separate finite alternative
const hole=[,];const explicit=[undefined];console.log(JSON.stringify({holeJSON:JSON.stringify(hole),explicitJSON:JSON.stringify(explicit),holePresent:0 in hole,explicitPresent:0 in explicit}));
Alternative output
{"holeJSON":"[null]","explicitJSON":"[null]","holePresent":false,"explicitPresent":true}

Work the boundary case

How many callbacks run after Array.from fills this array’s hole with undefined?

Show the worked answer

Three, at indexes 0, 1 and 2. The middle index is now present, even though its value is undefined.

Fresh boundary program
const values=Array.from([1,,undefined]);const visited=[];values.map((value,index)=>{visited.push(index);return value;});
console.log(JSON.stringify({middlePresent:1 in values,middleType:typeof values[1],visited}));
Boundary output
{"middlePresent":true,"middleType":"undefined","visited":[0,1,2]}

Original example files