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
|
show 1 more 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
5
Viewed almost 2500 times in 6 hours? It appears your friend is using the spread operator in a different context.
– Jeremy Harris
22 hours ago
This is more of a "how this expression is parsed" question. Type this in JS console and you'll notice that the 4th dot is colored differently... same color as zero.
– Salman A
6 hours ago
Always relevant
– MikeTheLiar
1 hour ago
|
show 1 more 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
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
New contributor
edited yesterday
New contributor
asked yesterday
Mist
17126
17126
New contributor
New contributor
5
Viewed almost 2500 times in 6 hours? It appears your friend is using the spread operator in a different context.
– Jeremy Harris
22 hours ago
This is more of a "how this expression is parsed" question. Type this in JS console and you'll notice that the 4th dot is colored differently... same color as zero.
– Salman A
6 hours ago
Always relevant
– MikeTheLiar
1 hour ago
|
show 1 more comment
5
Viewed almost 2500 times in 6 hours? It appears your friend is using the spread operator in a different context.
– Jeremy Harris
22 hours ago
This is more of a "how this expression is parsed" question. Type this in JS console and you'll notice that the 4th dot is colored differently... same color as zero.
– Salman A
6 hours ago
Always relevant
– MikeTheLiar
1 hour ago
5
5
Viewed almost 2500 times in 6 hours? It appears your friend is using the spread operator in a different context.
– Jeremy Harris
22 hours ago
Viewed almost 2500 times in 6 hours? It appears your friend is using the spread operator in a different context.
– Jeremy Harris
22 hours ago
This is more of a "how this expression is parsed" question. Type this in JS console and you'll notice that the 4th dot is colored differently... same color as zero.
– Salman A
6 hours ago
This is more of a "how this expression is parsed" question. Type this in JS console and you'll notice that the 4th dot is colored differently... same color as zero.
– Salman A
6 hours ago
Always relevant
– MikeTheLiar
1 hour ago
Always relevant
– MikeTheLiar
1 hour ago
|
show 1 more comment
3 Answers
3
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 {}
.
2
Oh, I just learned that you can spread a number. Wouldn't expect that. Thanks!
– Mist
yesterday
7
Spreading any number yields an empty object.
– Kresimir
yesterday
5
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
23 hours ago
2
@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
23 hours ago
1
NikxDa I think that @HiteshKumar made an important point. It's better to be more explicit about cases where your statements holds true.Spreading 0 (or any number) in object literal yields an empty object
Contains more useful information..
– Mist
16 hours ago
|
show 3 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
yesterday
2
@mist because numbers (and other primitives) get "boxed" into objects when you work with them as objects, and "unboxed" directly afterwards. Thereforex.k
will get lost.
– Jonas Wilms
yesterday
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
yesterday
1
Thanks! I see why my key on number couldn't work. Yayy boxing!
– Mist
yesterday
1
Numbers don't have any own enumerable properties. But they do have properties.
– Patrick Roberts
12 hours ago
|
show 4 more comments
In a simple terms {...}
spread operator in javascript extends one object/array with another.
So, when babelyfier tries extending one with another, it has to identify whether it is trying to extend an array or an object.
In the case of array
, it iterates over elements.
In the case of object
, it iterates over keys.
In this scenario, the babelyfier is trying to extract keys for number
by checking the Object's own property call
which is missing for number
so it returns empty Object.
add a 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
3 Answers
3
active
oldest
votes
3 Answers
3
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 {}
.
2
Oh, I just learned that you can spread a number. Wouldn't expect that. Thanks!
– Mist
yesterday
7
Spreading any number yields an empty object.
– Kresimir
yesterday
5
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
23 hours ago
2
@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
23 hours ago
1
NikxDa I think that @HiteshKumar made an important point. It's better to be more explicit about cases where your statements holds true.Spreading 0 (or any number) in object literal yields an empty object
Contains more useful information..
– Mist
16 hours ago
|
show 3 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 {}
.
2
Oh, I just learned that you can spread a number. Wouldn't expect that. Thanks!
– Mist
yesterday
7
Spreading any number yields an empty object.
– Kresimir
yesterday
5
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
23 hours ago
2
@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
23 hours ago
1
NikxDa I think that @HiteshKumar made an important point. It's better to be more explicit about cases where your statements holds true.Spreading 0 (or any number) in object literal yields an empty object
Contains more useful information..
– Mist
16 hours ago
|
show 3 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 15 hours ago
answered yesterday
NikxDa
2,69811531
2,69811531
2
Oh, I just learned that you can spread a number. Wouldn't expect that. Thanks!
– Mist
yesterday
7
Spreading any number yields an empty object.
– Kresimir
yesterday
5
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
23 hours ago
2
@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
23 hours ago
1
NikxDa I think that @HiteshKumar made an important point. It's better to be more explicit about cases where your statements holds true.Spreading 0 (or any number) in object literal yields an empty object
Contains more useful information..
– Mist
16 hours ago
|
show 3 more comments
2
Oh, I just learned that you can spread a number. Wouldn't expect that. Thanks!
– Mist
yesterday
7
Spreading any number yields an empty object.
– Kresimir
yesterday
5
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
23 hours ago
2
@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
23 hours ago
1
NikxDa I think that @HiteshKumar made an important point. It's better to be more explicit about cases where your statements holds true.Spreading 0 (or any number) in object literal yields an empty object
Contains more useful information..
– Mist
16 hours ago
2
2
Oh, I just learned that you can spread a number. Wouldn't expect that. Thanks!
– Mist
yesterday
Oh, I just learned that you can spread a number. Wouldn't expect that. Thanks!
– Mist
yesterday
7
7
Spreading any number yields an empty object.
– Kresimir
yesterday
Spreading any number yields an empty object.
– Kresimir
yesterday
5
5
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
23 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
23 hours ago
2
2
@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
23 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
23 hours ago
1
1
NikxDa I think that @HiteshKumar made an important point. It's better to be more explicit about cases where your statements holds true.
Spreading 0 (or any number) in object literal yields an empty object
Contains more useful information..– Mist
16 hours ago
NikxDa I think that @HiteshKumar made an important point. It's better to be more explicit about cases where your statements holds true.
Spreading 0 (or any number) in object literal yields an empty object
Contains more useful information..– Mist
16 hours ago
|
show 3 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
yesterday
2
@mist because numbers (and other primitives) get "boxed" into objects when you work with them as objects, and "unboxed" directly afterwards. Thereforex.k
will get lost.
– Jonas Wilms
yesterday
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
yesterday
1
Thanks! I see why my key on number couldn't work. Yayy boxing!
– Mist
yesterday
1
Numbers don't have any own enumerable properties. But they do have properties.
– Patrick Roberts
12 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
yesterday
2
@mist because numbers (and other primitives) get "boxed" into objects when you work with them as objects, and "unboxed" directly afterwards. Thereforex.k
will get lost.
– Jonas Wilms
yesterday
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
yesterday
1
Thanks! I see why my key on number couldn't work. Yayy boxing!
– Mist
yesterday
1
Numbers don't have any own enumerable properties. But they do have properties.
– Patrick Roberts
12 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.
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 yesterday
Jonas Wilms
54.6k42749
54.6k42749
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
yesterday
2
@mist because numbers (and other primitives) get "boxed" into objects when you work with them as objects, and "unboxed" directly afterwards. Thereforex.k
will get lost.
– Jonas Wilms
yesterday
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
yesterday
1
Thanks! I see why my key on number couldn't work. Yayy boxing!
– Mist
yesterday
1
Numbers don't have any own enumerable properties. But they do have properties.
– Patrick Roberts
12 hours ago
|
show 4 more comments
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
yesterday
2
@mist because numbers (and other primitives) get "boxed" into objects when you work with them as objects, and "unboxed" directly afterwards. Thereforex.k
will get lost.
– Jonas Wilms
yesterday
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
yesterday
1
Thanks! I see why my key on number couldn't work. Yayy boxing!
– Mist
yesterday
1
Numbers don't have any own enumerable properties. But they do have properties.
– Patrick Roberts
12 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
yesterday
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
yesterday
2
2
@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
yesterday
@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
yesterday
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
yesterday
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
yesterday
1
1
Thanks! I see why my key on number couldn't work. Yayy boxing!
– Mist
yesterday
Thanks! I see why my key on number couldn't work. Yayy boxing!
– Mist
yesterday
1
1
Numbers don't have any own enumerable properties. But they do have properties.
– Patrick Roberts
12 hours ago
Numbers don't have any own enumerable properties. But they do have properties.
– Patrick Roberts
12 hours ago
|
show 4 more comments
In a simple terms {...}
spread operator in javascript extends one object/array with another.
So, when babelyfier tries extending one with another, it has to identify whether it is trying to extend an array or an object.
In the case of array
, it iterates over elements.
In the case of object
, it iterates over keys.
In this scenario, the babelyfier is trying to extract keys for number
by checking the Object's own property call
which is missing for number
so it returns empty Object.
add a comment |
In a simple terms {...}
spread operator in javascript extends one object/array with another.
So, when babelyfier tries extending one with another, it has to identify whether it is trying to extend an array or an object.
In the case of array
, it iterates over elements.
In the case of object
, it iterates over keys.
In this scenario, the babelyfier is trying to extract keys for number
by checking the Object's own property call
which is missing for number
so it returns empty Object.
add a comment |
In a simple terms {...}
spread operator in javascript extends one object/array with another.
So, when babelyfier tries extending one with another, it has to identify whether it is trying to extend an array or an object.
In the case of array
, it iterates over elements.
In the case of object
, it iterates over keys.
In this scenario, the babelyfier is trying to extract keys for number
by checking the Object's own property call
which is missing for number
so it returns empty Object.
In a simple terms {...}
spread operator in javascript extends one object/array with another.
So, when babelyfier tries extending one with another, it has to identify whether it is trying to extend an array or an object.
In the case of array
, it iterates over elements.
In the case of object
, it iterates over keys.
In this scenario, the babelyfier is trying to extract keys for number
by checking the Object's own property call
which is missing for number
so it returns empty Object.
edited 4 hours ago
answered 4 hours ago
Rajendra kumar Vankadari
1,059911
1,059911
add a comment |
add a 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
5
Viewed almost 2500 times in 6 hours? It appears your friend is using the spread operator in a different context.
– Jeremy Harris
22 hours ago
This is more of a "how this expression is parsed" question. Type this in JS console and you'll notice that the 4th dot is colored differently... same color as zero.
– Salman A
6 hours ago
Always relevant
– MikeTheLiar
1 hour ago