These examples make missing indexes visible and compare arrays with array-like and iterable values. Each includes expected output and an explanation.
What does an array created with Array(3) contain?
const values = Array(3);
console.log(values.length);
console.log(Object.keys(values));
console.log(Object.hasOwn(values, 0));3
[]
false
The array has length 3 but no own indexed properties. Its three positions are holes.
Can a property-existence check distinguish a hole from an undefined value?
const sparse = ["first", , "third"];
const explicit = ["first", undefined, "third"];
console.log(sparse[1]);
console.log(explicit[1]);
console.log(Object.hasOwn(sparse, 1));
console.log(Object.hasOwn(explicit, 1));undefined
undefined
false
true
Both reads return undefined. Only the second array owns a property at index 1.
Does deleting an index make the array shorter?
const values = ["a", "b", "c"];
delete values[1];
console.log(values.length);
console.log(Object.hasOwn(values, 1));
console.log(Object.keys(values));3
false
[ '0', '2' ]
delete removes the property at index 1 but leaves the array length unchanged. Use splice if the goal is to remove the value and close the gap.
What happens when a value is assigned at index 3 of an empty array?
const values = [];
values[3] = "fourth";
console.log(values.length);
console.log(Object.keys(values));
console.log(Object.hasOwn(values, 1));4
[ '3' ]
false
The array length increases to 4. The indexes before 3 are holes.
How does changing length affect indexed properties?
const values = ["a", "b"];
values.length = 4;
console.log(values.length);
console.log(Object.keys(values));
values.length = 1;
console.log(values);
console.log(values.length);4
[ '0', '1' ]
[ 'a' ]
1
Increasing length adds holes. Decreasing length removes indexed properties at and above the new length.
How many times does map call its callback, and what happens to the hole?
const values = ["first", , "third"];
let calls = 0;
const mapped = values.map(function (value) {
calls += 1;
return value.toUpperCase();
});
console.log(calls);
console.log(mapped.length);
console.log(Object.hasOwn(mapped, 1));2
3
false
The callback runs for the two present values. The result keeps length 3 and has a hole at index 1.
Can findIndex match a hole when checking for undefined?
const values = ["first", , "third"];
const holeIndex = values.findIndex(function (value, index) {
return index === 1 && value === undefined;
});
console.log(holeIndex);1
The find family visits every index in its range. The callback receives undefined for the hole at index 1.
How do exact-value searches treat a hole?
const values = ["first", , "third"];
console.log(values.includes(undefined));
console.log(values.indexOf(undefined));
console.log(values.lastIndexOf(undefined));true
-1
-1
includes reads the hole as undefined. indexOf and lastIndexOf skip an ordinary hole.
How can a loop report every index and tell whether the array owns that index?
const values = ["first", , undefined];
for (let index = 0; index < values.length; index += 1) {
console.log(index, Object.hasOwn(values, index), values[index]);
}0 true first
1 false undefined
2 true undefined
The numeric loop visits every index below length. Object.hasOwn distinguishes the hole at index 1 from the explicit undefined at index 2.
What values does for...of read from a sparse array?
const values = ["first", , "third"];
for (const value of values) {
console.log(value);
}first
undefined
third
The array iterator visits positions from 0 through length minus 1. Reading the missing position produces undefined.
Do keys and entries omit a missing index?
const values = ["first", , "third"];
console.log(Array.from(values.keys()));
console.log(Array.from(values.entries()));[ 0, 1, 2 ]
[ [ 0, 'first' ], [ 1, undefined ], [ 2, 'third' ] ]
The iterators include every index in the array's range. entries reads the hole as undefined.
How are holes and explicit undefined represented by keys and JSON output?
const values = ["first", , undefined, "fourth"];
console.log(Object.keys(values));
console.log(JSON.stringify(values));[ '0', '2', '3' ]
["first",null,null,"fourth"]
Object.keys lists present enumerable indexes, so it omits the hole. JSON output represents both the hole and explicit undefined array value as null.
Does Array.from preserve a hole when copying an array?
const sparse = ["red", , "blue"];
const copied = Array.from(sparse);
console.log(copied);
console.log(Object.hasOwn(copied, 1));[ 'red', undefined, 'blue' ]
true
An array is iterable, and its iterator yields undefined at the missing position. Array.from creates an output property for every yielded value.
Can Array.from convert array-like values and map them in one step?
const source = {
0: "red",
1: "green",
length: 2
};
const result = Array.from(source, function (value) {
return value.toUpperCase();
});
console.log(result);[ 'RED', 'GREEN' ]
Array.from reads the array-like positions, applies its mapping callback, and returns a real array.
Does a length property make an object an actual array?
const listLike = {
0: "red",
1: "green",
length: 2
};
console.log(listLike.length);
console.log(listLike[0]);
console.log(Array.isArray(listLike));2
red
false
The object has indexed properties and a length, so it can be used as array-like data. It is not an actual array.
Can map create a result from a plain array-like object?
const source = {
0: "red",
1: "green",
length: 2
};
const uppercased = Array.prototype.map.call(source, function (value) {
return value.toUpperCase();
});
console.log(Array.isArray(uppercased));
console.log(uppercased);true
[ 'RED', 'GREEN' ]
map is generic and uses the receiver's length and indexed properties. It returns a new array.
What happens if an array-like object has a missing index?
const source = {
0: "red",
2: "blue",
length: 3
};
const uppercased = Array.prototype.map.call(source, function (value) {
return value.toUpperCase();
});
console.log(uppercased.length);
console.log(Object.hasOwn(uppercased, 1));
console.log(uppercased[2]);3
false
BLUE
map checks whether each index exists. It skips index 1 and preserves that hole in its result.
How can an iterable Set be turned into an array?
const tags = new Set(["html", "css", "html"]);
const tagArray = Array.from(tags);
console.log(typeof tags.length);
console.log(Array.isArray(tags));
console.log(tagArray);
console.log(Array.isArray(tagArray));undefined
false
[ 'html', 'css' ]
true
A Set is iterable but is not an array-like object with indexed values and a normal length. Array.from converts its unique values into an array.
Can spread syntax copy the values of a Set?
const tags = new Set(["html", "css"]);
const tagArray = [...tags];
console.log(tagArray);[ 'html', 'css' ]
A Set supplies an iterator, so spread syntax can read its values.
What happens when spread is used on an array-like object without an iterator?
const listLike = {
0: "red",
1: "green",
length: 2
};
try {
console.log([...listLike]);
} catch (error) {
console.log(error.name);
}TypeError
Spread syntax requires an iterable. The plain object has a length and indexes but does not supply Symbol.iterator. Use Array.from to convert it.
How can an array of four squares be generated from its indexes?
const squares = Array.from({ length: 4 }, function (value, index) {
return index * index;
});
console.log(squares);
console.log(Object.keys(squares));[ 0, 1, 4, 9 ]
[ '0', '1', '2', '3' ]
The mapper is called for each position in the array-like length range, so every index in the result has a value.
What is the difference between Array(3) and Array.of(3)?
const holes = Array(3);
const oneValue = Array.of(3);
console.log(holes.length, Object.keys(holes));
console.log(oneValue.length, oneValue[0]);3 []
1 3
Array(3) creates a length-3 array with holes. Array.of(3) creates a one-element array containing the number 3.
Which callbacks run for a hole in map, some, and findIndex?
const values = ["first", , "third"];
let mapCalls = 0;
let someCalls = 0;
let findIndexCalls = 0;
values.map(function (value) {
mapCalls += 1;
return value;
});
values.some(function (value) {
someCalls += 1;
return false;
});
values.findIndex(function (value, index) {
findIndexCalls += 1;
return index === 1 && value === undefined;
});
console.log(mapCalls);
console.log(someCalls);
console.log(findIndexCalls);2
2
2
map and some skip the hole and visit the two present entries. findIndex visits index 0, then the hole at index 1, where the callback matches and stops.
Which positions in this array are its own properties?
const values = ["a", , undefined];
for (let index = 0; index < values.length; index += 1) {
console.log(index, Object.hasOwn(values, index));
}0 true
1 false
2 true
The array owns indexes 0 and 2. Index 1 is a hole.
- Create an array with one hole and another with explicit undefined, then compare their own properties.
- Delete an index and compare length, Object.keys, and Object.hasOwn.
- Compare callback counts for map, some, and findIndex.
- Convert a sparse array with Array.from and check whether the result is dense.
- Convert a plain array-like object with Array.from.
- Try to spread a non-iterable array-like object, catch the error, and explain it.
- Convert a Set into an array and then call map on it.
- Borrow map for an array-like object using Array.prototype.map.call.
- Compare includes(undefined) and indexOf(undefined) on a hole.
- Serialize a sparse array with JSON.stringify and explain why the hole is not preserved in the JSON text.
| Previous chapter | Chapter lesson | Chapter Q&A | Next chapter |
|---|---|---|---|
| 50 - Sorting, comparators, and locale-aware comparison | Open lesson | Open Q&A | 52 - Objects: properties and methods |