Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does {. . . .0} evaluate to {}?

I just found {....0} in friend's code. Evaluating it in console returns {} (empty object).

Why is that? What is the meaning of 4 dots in JavaScript?

like image 804
Mist Avatar asked Dec 25 '18 11:12

Mist


2 Answers

Four dots actually have no meaning. ... is the spread operator, and .0 is short for 0.0.

Spreading 0 (or any number) into an object yields an empty object, therefore {}.

like image 54
NikxDa Avatar answered Sep 19 '22 21:09

NikxDa


Three dots in an object literal are a spread property, e.g.:

  const a = { b: 1, c: 1 };   const d = { ...a, e: 1 }; // { b: 1, c: 1, e: 1 } 

The last dot with a 0 is a number literal .0 is the same as 0.0. Therefore this:

 { ...(0.0) } 

spreads all properties of the number object into the object, however as numbers don't have any (own) properties you get back an empty object.

like image 30
Jonas Wilms Avatar answered Sep 23 '22 21:09

Jonas Wilms