JavaScript discussion forum, we believe that there is no such thing as a stupid question. Feel free to ask questions you have.

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

1 reply, 1 voice Last updated by  Admin 10 months, 3 weeks ago
  • Author
    Posts
  • #3509 Reply

    Admin
    Keymaster
    @ashwinsingh

    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?

  • #3510 Reply

    Admin
    Keymaster
    @ashwinsingh

    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.

Reply To: Why does {. . . .0} evaluate to {}?
Your information: