74

Why does spreading undefined in an object return an empty object? {...undefined} // equals {}:

console.log({...undefined})

And Why does spreading undefined in an array give you an error? [...undefined] // type error:

console.log([...undefined])
Gerardo Furtado
  • 100,839
  • 9
  • 121
  • 171
0xtimur
  • 1,163
  • 3
  • 10
  • 16

3 Answers3

93

As noted in the comments, and summarized by @ftor from #687, object spread is equivalent1 to Object.assign() (issues #687, #45), whereas spread in array literal context is iterable spread.

Quoting Ecma-262 6.0, Object.assign() is defined as:

19.1.2.1 Object.assign ( target, ...sources )

The assign function is used to copy the values of all of the enumerable own properties from one or more source objects to a target object. When the assign function is called, the following steps are taken:

  1. Let to be ToObject(target).
  2. ReturnIfAbrupt(to).
  3. If only one argument was passed, return to.
  4. Let sources be the List of argument values starting with the second argument.
  5. For each element nextSource of sources, in ascending index order, do
    1. If nextSource is undefined or null, let keys be an empty List.
    2. Else, ...

...followed by the description of copying own properties. The draft of Object Rest/Spread Properties is here. It is not a part of the Ecma-262 6.0.

A SpreadElement in an array literal expression is defined to begin as follows:

SpreadElement : ... AssignmentExpression

  1. Let spreadRef be the result of evaluating AssignmentExpression.
  2. Let spreadObj be GetValue(spreadRef).
  3. Let iterator be GetIterator(spreadObj).
  4. ReturnIfAbrupt(iterator).

And since undefined does not have a property with the key @@iterator, a TypeError is thrown, based on the steps of GetIterator. The standard is not an easy read, but if I'm not mistaken, the path to error is GetIterator -> GetMethod -> GetV -> ToObject, which throws a TypeError for undefined and null.

A simple remedy to using variables with possibly undefined value in array initialization is to use a default:

const maybeArray = undefined;
const newArray = [ ...(maybeArray || []) ];

1: There is a difference in how setters are handled.

Ilja Everilä
  • 50,538
  • 7
  • 126
  • 127
15

Normally, uses of ...x requires x to be iterable because the point of ... is normally to flatten an iterable into its components. An array is a prime example of an iterable.

undefined is not iterable. It has no components, so it doesn't make sense to iterate over undefined. [...undefined] therefore fails. for (const e of undefined) similarly fails, since of also requires an iterable.

However, {...x} requires x to be enumerable because it needs not just values, but keys along with them. An object is a prime example of an enumerable.

undefined is enumerable. undefined is sometime treated as an object, and this is one of those cases. Objects are enumerable because they can have properties. {...undefined} therefore succeeds. for (const p in undefined) similarly succeeds, since in requires an enumerable.


A note about null

A question asking about null's behaviour as an operand of ... was closed as a duplicate of this question. While null and undefined have distinct types, the above answers applies equally to null.

ikegami
  • 367,544
  • 15
  • 269
  • 518
-1

You may use this expression: [...(this.options || [])], where options is an array.

zcoop98
  • 2,590
  • 1
  • 18
  • 31
Maddy
  • 503
  • 6
  • 17
  • This in no way answers the question. not having sufficient permissions to post comments is not an excuse for posting it as an answer. – ikegami Nov 02 '22 at 19:19