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?
javascript spread-syntax number-literal
New contributor
Mist is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
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?
javascript spread-syntax number-literal
New contributor
Mist is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
4
in friends code ... but why ... ?
– Jonas Wilms
14 hours ago
12
Yeah, asking for a friend... ;)
– Kresimir
14 hours ago
2
Viewed almost 2500 times in 6 hours? It appears your friend is using the spread operator in a different context.
– Jeremy Harris
7 hours ago
1
@jeremy views on SO are exponential, at some point they are reblogged, posted on twitter and hacker news ... usually that happens to the "good to know, but useless in reality" questions ...
– Jonas Wilms
7 hours ago
add a comment |
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?
javascript spread-syntax number-literal
New contributor
Mist is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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?
javascript spread-syntax number-literal
javascript spread-syntax number-literal
New contributor
Mist is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Mist is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 10 hours ago
New contributor
Mist is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 14 hours ago
Mist
10816
10816
New contributor
Mist is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Mist is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Mist is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
4
in friends code ... but why ... ?
– Jonas Wilms
14 hours ago
12
Yeah, asking for a friend... ;)
– Kresimir
14 hours ago
2
Viewed almost 2500 times in 6 hours? It appears your friend is using the spread operator in a different context.
– Jeremy Harris
7 hours ago
1
@jeremy views on SO are exponential, at some point they are reblogged, posted on twitter and hacker news ... usually that happens to the "good to know, but useless in reality" questions ...
– Jonas Wilms
7 hours ago
add a comment |
4
in friends code ... but why ... ?
– Jonas Wilms
14 hours ago
12
Yeah, asking for a friend... ;)
– Kresimir
14 hours ago
2
Viewed almost 2500 times in 6 hours? It appears your friend is using the spread operator in a different context.
– Jeremy Harris
7 hours ago
1
@jeremy views on SO are exponential, at some point they are reblogged, posted on twitter and hacker news ... usually that happens to the "good to know, but useless in reality" questions ...
– Jonas Wilms
7 hours ago
4
4
in friends code ... but why ... ?
– Jonas Wilms
14 hours ago
in friends code ... but why ... ?
– Jonas Wilms
14 hours ago
12
12
Yeah, asking for a friend... ;)
– Kresimir
14 hours ago
Yeah, asking for a friend... ;)
– Kresimir
14 hours ago
2
2
Viewed almost 2500 times in 6 hours? It appears your friend is using the spread operator in a different context.
– Jeremy Harris
7 hours ago
Viewed almost 2500 times in 6 hours? It appears your friend is using the spread operator in a different context.
– Jeremy Harris
7 hours ago
1
1
@jeremy views on SO are exponential, at some point they are reblogged, posted on twitter and hacker news ... usually that happens to the "good to know, but useless in reality" questions ...
– Jonas Wilms
7 hours ago
@jeremy views on SO are exponential, at some point they are reblogged, posted on twitter and hacker news ... usually that happens to the "good to know, but useless in reality" questions ...
– Jonas Wilms
7 hours ago
add a comment |
2 Answers
2
active
oldest
votes
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 {}.
1
Oh, I just learned that you can spread a number. Wouldn't expect that. Thanks!
– Mist
14 hours ago
6
Spreading any number yields an empty object.
– Kresimir
14 hours ago
4
Spreading 0 (or any number) yields an empty objectnot necessarily if you spread a number at any other places apart from an object, it will throw an error eg [...0] throws an error.
– Hitesh Kumar
9 hours ago
@HiteshKumar Spreading non-iterable objects inside an array will indeed throw an error, but that has nothing to do with this question. I am referring to the object-spread mentioned. :)
– NikxDa
9 hours ago
Gotcha. Thanks for the clarification.
– Hitesh Kumar
9 hours ago
|
show 4 more comments
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 properties you get back an empty object.
You lead me to thinking - I can spread any variable, and own keys will be spread into the new object? It works forFunction(function x() {}), (x.k = 'v'), ({...x})// {k: 'v'}but doesn't work forNumber(x = 10), (x.k = 'v'), ({...x}) // {}
– Mist
14 hours ago
1
@mist because numbers (and other primitives) get "boxed" into objects when you work with them as objects, and "unboxed" directly afterwards. Thereforex.kwill get lost.
– Jonas Wilms
14 hours ago
What does 'boxed' means exactly? E.g. when I used the dot operator (property) I worked with the number as object. If I am correct, that's just one case. Are there other cases when 'boxing' is happening? Does it apply only to numbers? Is there a perf reason or something? I guess this is for other question, and I should study it further. Could you point me to some book or something?
– Mist
14 hours ago
@mist It applies to all primitives (number, boolean, string), and it means thatx.kis the same as(new Number(x)).x, and thats because JavaScript's design philosophy is everything is an object and thats how they implemented it (not the most elegant solution though in my eyes)
– Jonas Wilms
14 hours ago
1
Thanks! I see why my key on number couldn't work. Yayy boxing!
– Mist
14 hours ago
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Mist is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53922010%2fwhy-does-0-evaluate-to%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 {}.
1
Oh, I just learned that you can spread a number. Wouldn't expect that. Thanks!
– Mist
14 hours ago
6
Spreading any number yields an empty object.
– Kresimir
14 hours ago
4
Spreading 0 (or any number) yields an empty objectnot necessarily if you spread a number at any other places apart from an object, it will throw an error eg [...0] throws an error.
– Hitesh Kumar
9 hours ago
@HiteshKumar Spreading non-iterable objects inside an array will indeed throw an error, but that has nothing to do with this question. I am referring to the object-spread mentioned. :)
– NikxDa
9 hours ago
Gotcha. Thanks for the clarification.
– Hitesh Kumar
9 hours ago
|
show 4 more comments
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 {}.
1
Oh, I just learned that you can spread a number. Wouldn't expect that. Thanks!
– Mist
14 hours ago
6
Spreading any number yields an empty object.
– Kresimir
14 hours ago
4
Spreading 0 (or any number) yields an empty objectnot necessarily if you spread a number at any other places apart from an object, it will throw an error eg [...0] throws an error.
– Hitesh Kumar
9 hours ago
@HiteshKumar Spreading non-iterable objects inside an array will indeed throw an error, but that has nothing to do with this question. I am referring to the object-spread mentioned. :)
– NikxDa
9 hours ago
Gotcha. Thanks for the clarification.
– Hitesh Kumar
9 hours ago
|
show 4 more comments
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 {}.
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 {}.
edited 1 hour ago
answered 14 hours ago
NikxDa
2,53811429
2,53811429
1
Oh, I just learned that you can spread a number. Wouldn't expect that. Thanks!
– Mist
14 hours ago
6
Spreading any number yields an empty object.
– Kresimir
14 hours ago
4
Spreading 0 (or any number) yields an empty objectnot necessarily if you spread a number at any other places apart from an object, it will throw an error eg [...0] throws an error.
– Hitesh Kumar
9 hours ago
@HiteshKumar Spreading non-iterable objects inside an array will indeed throw an error, but that has nothing to do with this question. I am referring to the object-spread mentioned. :)
– NikxDa
9 hours ago
Gotcha. Thanks for the clarification.
– Hitesh Kumar
9 hours ago
|
show 4 more comments
1
Oh, I just learned that you can spread a number. Wouldn't expect that. Thanks!
– Mist
14 hours ago
6
Spreading any number yields an empty object.
– Kresimir
14 hours ago
4
Spreading 0 (or any number) yields an empty objectnot necessarily if you spread a number at any other places apart from an object, it will throw an error eg [...0] throws an error.
– Hitesh Kumar
9 hours ago
@HiteshKumar Spreading non-iterable objects inside an array will indeed throw an error, but that has nothing to do with this question. I am referring to the object-spread mentioned. :)
– NikxDa
9 hours ago
Gotcha. Thanks for the clarification.
– Hitesh Kumar
9 hours ago
1
1
Oh, I just learned that you can spread a number. Wouldn't expect that. Thanks!
– Mist
14 hours ago
Oh, I just learned that you can spread a number. Wouldn't expect that. Thanks!
– Mist
14 hours ago
6
6
Spreading any number yields an empty object.
– Kresimir
14 hours ago
Spreading any number yields an empty object.
– Kresimir
14 hours ago
4
4
Spreading 0 (or any number) yields an empty object not necessarily if you spread a number at any other places apart from an object, it will throw an error eg [...0] throws an error.– Hitesh Kumar
9 hours ago
Spreading 0 (or any number) yields an empty object not necessarily if you spread a number at any other places apart from an object, it will throw an error eg [...0] throws an error.– Hitesh Kumar
9 hours ago
@HiteshKumar Spreading non-iterable objects inside an array will indeed throw an error, but that has nothing to do with this question. I am referring to the object-spread mentioned. :)
– NikxDa
9 hours ago
@HiteshKumar Spreading non-iterable objects inside an array will indeed throw an error, but that has nothing to do with this question. I am referring to the object-spread mentioned. :)
– NikxDa
9 hours ago
Gotcha. Thanks for the clarification.
– Hitesh Kumar
9 hours ago
Gotcha. Thanks for the clarification.
– Hitesh Kumar
9 hours ago
|
show 4 more comments
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 properties you get back an empty object.
You lead me to thinking - I can spread any variable, and own keys will be spread into the new object? It works forFunction(function x() {}), (x.k = 'v'), ({...x})// {k: 'v'}but doesn't work forNumber(x = 10), (x.k = 'v'), ({...x}) // {}
– Mist
14 hours ago
1
@mist because numbers (and other primitives) get "boxed" into objects when you work with them as objects, and "unboxed" directly afterwards. Thereforex.kwill get lost.
– Jonas Wilms
14 hours ago
What does 'boxed' means exactly? E.g. when I used the dot operator (property) I worked with the number as object. If I am correct, that's just one case. Are there other cases when 'boxing' is happening? Does it apply only to numbers? Is there a perf reason or something? I guess this is for other question, and I should study it further. Could you point me to some book or something?
– Mist
14 hours ago
@mist It applies to all primitives (number, boolean, string), and it means thatx.kis the same as(new Number(x)).x, and thats because JavaScript's design philosophy is everything is an object and thats how they implemented it (not the most elegant solution though in my eyes)
– Jonas Wilms
14 hours ago
1
Thanks! I see why my key on number couldn't work. Yayy boxing!
– Mist
14 hours ago
|
show 1 more comment
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 properties you get back an empty object.
You lead me to thinking - I can spread any variable, and own keys will be spread into the new object? It works forFunction(function x() {}), (x.k = 'v'), ({...x})// {k: 'v'}but doesn't work forNumber(x = 10), (x.k = 'v'), ({...x}) // {}
– Mist
14 hours ago
1
@mist because numbers (and other primitives) get "boxed" into objects when you work with them as objects, and "unboxed" directly afterwards. Thereforex.kwill get lost.
– Jonas Wilms
14 hours ago
What does 'boxed' means exactly? E.g. when I used the dot operator (property) I worked with the number as object. If I am correct, that's just one case. Are there other cases when 'boxing' is happening? Does it apply only to numbers? Is there a perf reason or something? I guess this is for other question, and I should study it further. Could you point me to some book or something?
– Mist
14 hours ago
@mist It applies to all primitives (number, boolean, string), and it means thatx.kis the same as(new Number(x)).x, and thats because JavaScript's design philosophy is everything is an object and thats how they implemented it (not the most elegant solution though in my eyes)
– Jonas Wilms
14 hours ago
1
Thanks! I see why my key on number couldn't work. Yayy boxing!
– Mist
14 hours ago
|
show 1 more comment
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 properties you get back an empty object.
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 properties you get back an empty object.
answered 14 hours ago
Jonas Wilms
54.4k42649
54.4k42649
You lead me to thinking - I can spread any variable, and own keys will be spread into the new object? It works forFunction(function x() {}), (x.k = 'v'), ({...x})// {k: 'v'}but doesn't work forNumber(x = 10), (x.k = 'v'), ({...x}) // {}
– Mist
14 hours ago
1
@mist because numbers (and other primitives) get "boxed" into objects when you work with them as objects, and "unboxed" directly afterwards. Thereforex.kwill get lost.
– Jonas Wilms
14 hours ago
What does 'boxed' means exactly? E.g. when I used the dot operator (property) I worked with the number as object. If I am correct, that's just one case. Are there other cases when 'boxing' is happening? Does it apply only to numbers? Is there a perf reason or something? I guess this is for other question, and I should study it further. Could you point me to some book or something?
– Mist
14 hours ago
@mist It applies to all primitives (number, boolean, string), and it means thatx.kis the same as(new Number(x)).x, and thats because JavaScript's design philosophy is everything is an object and thats how they implemented it (not the most elegant solution though in my eyes)
– Jonas Wilms
14 hours ago
1
Thanks! I see why my key on number couldn't work. Yayy boxing!
– Mist
14 hours ago
|
show 1 more comment
You lead me to thinking - I can spread any variable, and own keys will be spread into the new object? It works forFunction(function x() {}), (x.k = 'v'), ({...x})// {k: 'v'}but doesn't work forNumber(x = 10), (x.k = 'v'), ({...x}) // {}
– Mist
14 hours ago
1
@mist because numbers (and other primitives) get "boxed" into objects when you work with them as objects, and "unboxed" directly afterwards. Thereforex.kwill get lost.
– Jonas Wilms
14 hours ago
What does 'boxed' means exactly? E.g. when I used the dot operator (property) I worked with the number as object. If I am correct, that's just one case. Are there other cases when 'boxing' is happening? Does it apply only to numbers? Is there a perf reason or something? I guess this is for other question, and I should study it further. Could you point me to some book or something?
– Mist
14 hours ago
@mist It applies to all primitives (number, boolean, string), and it means thatx.kis the same as(new Number(x)).x, and thats because JavaScript's design philosophy is everything is an object and thats how they implemented it (not the most elegant solution though in my eyes)
– Jonas Wilms
14 hours ago
1
Thanks! I see why my key on number couldn't work. Yayy boxing!
– Mist
14 hours ago
You lead me to thinking - I can spread any variable, and own keys will be spread into the new object? It works for
Function (function x() {}), (x.k = 'v'), ({...x})// {k: 'v'} but doesn't work for Number (x = 10), (x.k = 'v'), ({...x}) // {}– Mist
14 hours ago
You lead me to thinking - I can spread any variable, and own keys will be spread into the new object? It works for
Function (function x() {}), (x.k = 'v'), ({...x})// {k: 'v'} but doesn't work for Number (x = 10), (x.k = 'v'), ({...x}) // {}– Mist
14 hours ago
1
1
@mist because numbers (and other primitives) get "boxed" into objects when you work with them as objects, and "unboxed" directly afterwards. Therefore
x.k will get lost.– Jonas Wilms
14 hours ago
@mist because numbers (and other primitives) get "boxed" into objects when you work with them as objects, and "unboxed" directly afterwards. Therefore
x.k will get lost.– Jonas Wilms
14 hours ago
What does 'boxed' means exactly? E.g. when I used the dot operator (property) I worked with the number as object. If I am correct, that's just one case. Are there other cases when 'boxing' is happening? Does it apply only to numbers? Is there a perf reason or something? I guess this is for other question, and I should study it further. Could you point me to some book or something?
– Mist
14 hours ago
What does 'boxed' means exactly? E.g. when I used the dot operator (property) I worked with the number as object. If I am correct, that's just one case. Are there other cases when 'boxing' is happening? Does it apply only to numbers? Is there a perf reason or something? I guess this is for other question, and I should study it further. Could you point me to some book or something?
– Mist
14 hours ago
@mist It applies to all primitives (number, boolean, string), and it means that
x.k is the same as (new Number(x)).x, and thats because JavaScript's design philosophy is everything is an object and thats how they implemented it (not the most elegant solution though in my eyes)– Jonas Wilms
14 hours ago
@mist It applies to all primitives (number, boolean, string), and it means that
x.k is the same as (new Number(x)).x, and thats because JavaScript's design philosophy is everything is an object and thats how they implemented it (not the most elegant solution though in my eyes)– Jonas Wilms
14 hours ago
1
1
Thanks! I see why my key on number couldn't work. Yayy boxing!
– Mist
14 hours ago
Thanks! I see why my key on number couldn't work. Yayy boxing!
– Mist
14 hours ago
|
show 1 more comment
Mist is a new contributor. Be nice, and check out our Code of Conduct.
Mist is a new contributor. Be nice, and check out our Code of Conduct.
Mist is a new contributor. Be nice, and check out our Code of Conduct.
Mist is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53922010%2fwhy-does-0-evaluate-to%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
4
in friends code ... but why ... ?
– Jonas Wilms
14 hours ago
12
Yeah, asking for a friend... ;)
– Kresimir
14 hours ago
2
Viewed almost 2500 times in 6 hours? It appears your friend is using the spread operator in a different context.
– Jeremy Harris
7 hours ago
1
@jeremy views on SO are exponential, at some point they are reblogged, posted on twitter and hacker news ... usually that happens to the "good to know, but useless in reality" questions ...
– Jonas Wilms
7 hours ago