Engineering / Field note
A sparse array can skip your validation callback
Array length does not prove that every position contains a row. Five JavaScript examples show missing slots, inherited indexes and validation before side effects.
A JavaScript array can report a length of three while containing only two indexed entries. That matters when validation lives inside an iteration callback: a missing position may never reach the validator. Array length describes an index range; it is not a count of populated rows. [1]
This reference note was reviewed on November 30, 2026 against the current ECMAScript specification draft, whose site identifies it as ECMAScript 2027. The behavior discussed here is established language behavior, not a new release or vulnerability announcement. Our five original examples use fictional unit records and ordinary arrays to make the boundaries visible.
Observe which indexes map actually visits
Array.prototype.map checks whether an indexed property exists before invoking its callback. With ordinary, unmodified array prototypes, a hole is skipped. The result retains a missing position at that index. In the first example, the comma gap is intentional: the callback visits indexes zero and two, and the mapped array still has length three. [2]
const entries = [{ units: 2 }, , { units: 4 }];
const visited = [];
const doubled = entries.map((row, index) => {
visited.push(index);
return { units: row.units * 2 };
});
const observation = {
length: doubled.length,
visited,
ownsMiddle: Object.hasOwn(doubled, 1)
};Moving a row check into that callback would not make the middle position appear. This is why “the transformation completed” and “every required position was validated” are different claims. The visited list records the work performed, while ownsMiddle checks the shape of the result.
An all-holes array can make every return true
The every method also checks property presence. It returns false if a visited predicate fails and otherwise returns true. In our second example, new Array(3) creates the range without any indexed entries. Under the ordinary prototype used here, the predicate runs zero times and accepted becomes true. [2]
const entries = new Array(3);
let calls = 0;
const accepted = entries.every(row => {
calls += 1;
return Number.isSafeInteger(row?.units);
});
const observation = {
accepted,
calls,
length: entries.length
};That result is consistent with the method’s contract. The mistake would be using it as proof of three valid rows. A business rule may require a nonempty list and one supplied row at every index; those are additional conditions that the application has to express.
Require own positions when that is the input contract
There is an important qualification: HasProperty includes inherited properties. An index supplied by a prototype can therefore be visited by map. Object.hasOwn answers the narrower question of whether the object itself supplies the property. Our next example adds a numeric property to a dedicated prototype, leaving the global Array.prototype untouched. [3] [4]
function hasOwnSlots(array) {
for (let index = 0; index < array.length; index += 1) {
if (!Object.hasOwn(array, index)) return false;
}
return true;
}
const inherited = new Array(1);
const prototype = Object.create(Array.prototype);
prototype[0] = { units: 9 };
Object.setPrototypeOf(inherited, prototype);
const mapped = inherited.map(row => row.units);
const observation = {
mappedInherited: mapped[0],
inheritedHasOwnSlots: hasOwnSlots(inherited),
explicitUndefinedHasOwnSlots: hasOwnSlots([undefined]),
holeHasOwnSlots: hasOwnSlots(new Array(1))
};The inherited row maps to nine, yet hasOwnSlots rejects it. An explicit undefined entry passes the same shape check because the position exists. Its value may still be invalid. This separates two useful questions: did the caller supply every required position, and does each supplied value meet the row rules?
JSON changes the representation; it does not validate the rows
A plain JSON.parse call without a reviver does not accept the JavaScript elision syntax [1,,3]. In the ordinary serialization case below, JSON.stringify represents the missing array entry as null. Parsing that text produces an own property containing null. The round trip changes a shape problem into a value that still needs checking. [5]
const entries = [{ units: 2 }, , { units: 4 }];
const wire = JSON.stringify(entries);
const decoded = JSON.parse(wire);
let rejectsElision = false;
try {
JSON.parse('[1,,3]');
} catch (error) {
rejectsElision = error instanceof SyntaxError;
}
const observation = {
wire,
ownsMiddle: Object.hasOwn(decoded, 1),
middleIsNull: decoded[1] === null,
rejectsElision
};This distinction keeps the diagnosis honest. A typical JSON request body cannot directly spell a sparse array using a double comma. Sparse arrays may instead arise in JavaScript construction or later mutation. Conversely, a dense decoded array can contain null, a string or another unsuitable row. Density alone does not establish valid content.
Finish validation before performing the business operation
Our final example states a small fictional contract: one to one thousand rows, every position owned by the array, and each row carrying a positive safe-integer units field. It checks the shape first, validates the values and returns fresh records. The function performs no network, storage or business writes.
function validateRows(input) {
if (!Array.isArray(input)) throw new TypeError('Expected array');
const length = input.length;
if (length < 1 || length > 1000) {
throw new RangeError('Expected 1 to 1000 rows');
}
for (let index = 0; index < length; index += 1) {
if (!Object.hasOwn(input, index)) {
throw new TypeError('Missing row at ' + index);
}
}
const checked = [];
for (let index = 0; index < length; index += 1) {
const row = input[index];
if (row === null || typeof row !== 'object' ||
Array.isArray(row) || !Object.hasOwn(row, 'units')) {
throw new TypeError('Invalid row at ' + index);
}
const units = row.units;
if (!Number.isSafeInteger(units) || units < 1) {
throw new TypeError('Invalid units at ' + index);
}
checked.push({ units });
}
return checked;
}
const rows = validateRows(JSON.parse('[{"units":2},{"units":4}]'));
const observation = { rows, ownsSecond: Object.hasOwn(rows, 1) };The intended inputs are inert data objects, such as the result of ordinary JSON decoding. This example is not a sandbox for arbitrary proxies, getters or callbacks that can mutate data while it is inspected. Those require their own trust-boundary design. Its size limit is our illustrative application rule, not a limit imposed by JavaScript.
We executed all five examples with Node.js 24.22.1 and checked their stated observations, including inherited-index behavior and rejection of sparse or invalid rows by the final function. For an application, use the returned records only after validation succeeds, then handle authorization and the business operation separately. A completed callback pass becomes useful evidence once the application has established which positions that pass was required to cover.
Sources + further reading
Source dates describe the original material. Publication above is the date this Zyel article went live.
- ECMAScript: array exotic objects and lengthEcma International / TC39 · Checked Nov 30, 2026
- ECMAScript: Array.prototype.map and array iterationEcma International / TC39 · Checked Nov 30, 2026
- ECMAScript: HasProperty and HasOwnPropertyEcma International / TC39 · Checked Nov 30, 2026
- ECMAScript: Object.hasOwnEcma International / TC39 · Checked Nov 30, 2026
- ECMAScript: JSON parsing and array serializationEcma International / TC39 · Checked Nov 30, 2026
Reader discussion
What would you ask next?
Questions, useful corrections and thoughtful disagreement are welcome. Comments are reviewed before they appear.
No reader comments yet.