Propertyempty slotundefined
How it is createdCreated by sparse array syntax such as new Array(5)Explicit assignment like arr[0] = undefined
Whether a value existsNo value is stored at allA value exists, and that value is undefined
Access resultarr[i] // undefined because the slot does not existarr[i] // undefined
in operatori in arr // falsei in arr // true
hasOwnPropertyarr.hasOwnProperty(i) // falsearr.hasOwnProperty(i) // true
JSON.stringifyEmpty slots may be skippedThe position is preserved as a value
map / forEach iterationThat index is skippedThat index is iterated normally
Example difference[ , , 3] -> length = 3, but the first two positions are empty slots[undefined, undefined, 3] -> length = 3, and the first two positions do contain values

Example

const arr1 = new Array(3); // [ <3 empty items> ]
const arr2 = [undefined, undefined, undefined];
console.log(0 in arr1); // false -> the slot does not exist
console.log(0 in arr2); // true -> the value exists and is undefined
console.log(arr1.hasOwnProperty(0)); // false
console.log(arr2.hasOwnProperty(0)); // true
arr1.forEach((x, i) => console.log(i, x)); // prints nothing
arr2.forEach((x, i) => console.log(i, x)); // prints 0 undefined, 1 undefined, 2 undefined

Quick memory trick:

  • empty slot: no stored value, the position is absent
  • undefined: a stored value exists, but that value is empty