Array holes, named keys and inherited keys
Predict both lines without treating for…in as an index loop.
Code Snippet
const list = [10, , 30];
list.note = "extra";
const proto = Object.create(Array.prototype);
proto.inherited = "parent";
Object.setPrototypeOf(list, proto);
const keys = [], values = [];
for (const key in list) keys.push(key);
for (const value of list) values.push(String(value));
console.log(keys.join(" "));
console.log(values.join(" "));Expected Output & Explanation
0 2 note inherited
10 undefined 30 for…in sees enumerable string keys up the prototype chain, but not absent indices or non-enumerable length. The array iterator reads numeric positions from zero through length minus one, yielding undefined for the missing index here.
Module Context & References
JavaScript · Language output drills Trace closures, receivers, prototypes, coercion and array edge cases
Personal Notes · Markdown