How is “for(const [[[[[a, b, c]]]]] in [0, 0]);” even valid?
up vote
22
down vote
favorite
Writing dumb code, I've just found something weird.
for(const [[[[[fancy, loop]]]]] in [0, 0]) {
console.log(fancy, loop);
}
// Chrome 70.0.3538.77 says:
// 0 undefined
// 1 undefined
It's like assigning 0
and 1
to [[[[[fancy, loop]]]]]
, which, is array destructurings taking place and supposed to throw an error, isn't it? Or not. It's just my thought getting me confused right now.
Could you please tell me how it is valid and works with no error? What am I missing?
javascript
add a comment |
up vote
22
down vote
favorite
Writing dumb code, I've just found something weird.
for(const [[[[[fancy, loop]]]]] in [0, 0]) {
console.log(fancy, loop);
}
// Chrome 70.0.3538.77 says:
// 0 undefined
// 1 undefined
It's like assigning 0
and 1
to [[[[[fancy, loop]]]]]
, which, is array destructurings taking place and supposed to throw an error, isn't it? Or not. It's just my thought getting me confused right now.
Could you please tell me how it is valid and works with no error? What am I missing?
javascript
@RobG Yeah, but, how could it destructure0
and1
?
– K._
Nov 5 at 0:25
2
you can always throw the code into a transpiler to see what is happening
– Bravo
Nov 5 at 0:25
2
babeljs.io/repl/…
– Bravo
Nov 5 at 0:26
17
I guess you discovered a new question for javascript job interviews. ;-)
– RobG
Nov 5 at 0:49
4
^ @RobG for anyone wondering, this should be taken as sarcasm
– Pierre Arlaud
Nov 5 at 9:28
add a comment |
up vote
22
down vote
favorite
up vote
22
down vote
favorite
Writing dumb code, I've just found something weird.
for(const [[[[[fancy, loop]]]]] in [0, 0]) {
console.log(fancy, loop);
}
// Chrome 70.0.3538.77 says:
// 0 undefined
// 1 undefined
It's like assigning 0
and 1
to [[[[[fancy, loop]]]]]
, which, is array destructurings taking place and supposed to throw an error, isn't it? Or not. It's just my thought getting me confused right now.
Could you please tell me how it is valid and works with no error? What am I missing?
javascript
Writing dumb code, I've just found something weird.
for(const [[[[[fancy, loop]]]]] in [0, 0]) {
console.log(fancy, loop);
}
// Chrome 70.0.3538.77 says:
// 0 undefined
// 1 undefined
It's like assigning 0
and 1
to [[[[[fancy, loop]]]]]
, which, is array destructurings taking place and supposed to throw an error, isn't it? Or not. It's just my thought getting me confused right now.
Could you please tell me how it is valid and works with no error? What am I missing?
for(const [[[[[fancy, loop]]]]] in [0, 0]) {
console.log(fancy, loop);
}
for(const [[[[[fancy, loop]]]]] in [0, 0]) {
console.log(fancy, loop);
}
javascript
javascript
edited Nov 5 at 0:23
asked Nov 5 at 0:20
K._
2,29521436
2,29521436
@RobG Yeah, but, how could it destructure0
and1
?
– K._
Nov 5 at 0:25
2
you can always throw the code into a transpiler to see what is happening
– Bravo
Nov 5 at 0:25
2
babeljs.io/repl/…
– Bravo
Nov 5 at 0:26
17
I guess you discovered a new question for javascript job interviews. ;-)
– RobG
Nov 5 at 0:49
4
^ @RobG for anyone wondering, this should be taken as sarcasm
– Pierre Arlaud
Nov 5 at 9:28
add a comment |
@RobG Yeah, but, how could it destructure0
and1
?
– K._
Nov 5 at 0:25
2
you can always throw the code into a transpiler to see what is happening
– Bravo
Nov 5 at 0:25
2
babeljs.io/repl/…
– Bravo
Nov 5 at 0:26
17
I guess you discovered a new question for javascript job interviews. ;-)
– RobG
Nov 5 at 0:49
4
^ @RobG for anyone wondering, this should be taken as sarcasm
– Pierre Arlaud
Nov 5 at 9:28
@RobG Yeah, but, how could it destructure
0
and 1
?– K._
Nov 5 at 0:25
@RobG Yeah, but, how could it destructure
0
and 1
?– K._
Nov 5 at 0:25
2
2
you can always throw the code into a transpiler to see what is happening
– Bravo
Nov 5 at 0:25
you can always throw the code into a transpiler to see what is happening
– Bravo
Nov 5 at 0:25
2
2
babeljs.io/repl/…
– Bravo
Nov 5 at 0:26
babeljs.io/repl/…
– Bravo
Nov 5 at 0:26
17
17
I guess you discovered a new question for javascript job interviews. ;-)
– RobG
Nov 5 at 0:49
I guess you discovered a new question for javascript job interviews. ;-)
– RobG
Nov 5 at 0:49
4
4
^ @RobG for anyone wondering, this should be taken as sarcasm
– Pierre Arlaud
Nov 5 at 9:28
^ @RobG for anyone wondering, this should be taken as sarcasm
– Pierre Arlaud
Nov 5 at 9:28
add a comment |
2 Answers
2
active
oldest
votes
up vote
21
down vote
accepted
It's not assigning 0
and 1
to [[[[[fancy, loop]]]]]
. You're looping over the keys of [0, 0]
, because you used in
instead of of
, and those keys are strings.
The string "0"
is an iterable whose sole element is "0"
. Assigning "0"
to [[[[[fancy, loop]]]]]
repeatedly unpacks "0"
and gets "0"
, until eventually it gets down to
[fancy, loop] = "0"
at which point the final unpacking assigns "0"
to fancy
and undefined
to loop
.
1
This doesn't fully explain what is occurring though, there is a difference between[fancy, loop]
and[[fancy, loop]]
given[,,,,,,,,,0,0]
(i.e. try it with property names of more than one character). The assignments are different so it doesn't simply reduce to[fancy, loop]
.
– RobG
Nov 5 at 0:54
1
@RobG: You skipped the first four layers of unpacking, each of which only takes one element."10"
gets unpacked into"1"
and"0"
, and the"0"
gets discarded in the first layer of the destructuring assignment. By the time we hit the[fancy, loop]
unpacking, the"0"
has been gone for the last three layers.
– user2357112
Nov 5 at 1:12
Yes, that's the missing part. ;-)
– RobG
Nov 5 at 1:13
add a comment |
up vote
4
down vote
You're using in
instead of of
so you're getting the properties of the array not the values. For this case you're getting the array indexes as strings (0
, 1
). You're basically destructuring a string with length of 1 every time. So you always get the first character of every iterated property
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
21
down vote
accepted
It's not assigning 0
and 1
to [[[[[fancy, loop]]]]]
. You're looping over the keys of [0, 0]
, because you used in
instead of of
, and those keys are strings.
The string "0"
is an iterable whose sole element is "0"
. Assigning "0"
to [[[[[fancy, loop]]]]]
repeatedly unpacks "0"
and gets "0"
, until eventually it gets down to
[fancy, loop] = "0"
at which point the final unpacking assigns "0"
to fancy
and undefined
to loop
.
1
This doesn't fully explain what is occurring though, there is a difference between[fancy, loop]
and[[fancy, loop]]
given[,,,,,,,,,0,0]
(i.e. try it with property names of more than one character). The assignments are different so it doesn't simply reduce to[fancy, loop]
.
– RobG
Nov 5 at 0:54
1
@RobG: You skipped the first four layers of unpacking, each of which only takes one element."10"
gets unpacked into"1"
and"0"
, and the"0"
gets discarded in the first layer of the destructuring assignment. By the time we hit the[fancy, loop]
unpacking, the"0"
has been gone for the last three layers.
– user2357112
Nov 5 at 1:12
Yes, that's the missing part. ;-)
– RobG
Nov 5 at 1:13
add a comment |
up vote
21
down vote
accepted
It's not assigning 0
and 1
to [[[[[fancy, loop]]]]]
. You're looping over the keys of [0, 0]
, because you used in
instead of of
, and those keys are strings.
The string "0"
is an iterable whose sole element is "0"
. Assigning "0"
to [[[[[fancy, loop]]]]]
repeatedly unpacks "0"
and gets "0"
, until eventually it gets down to
[fancy, loop] = "0"
at which point the final unpacking assigns "0"
to fancy
and undefined
to loop
.
1
This doesn't fully explain what is occurring though, there is a difference between[fancy, loop]
and[[fancy, loop]]
given[,,,,,,,,,0,0]
(i.e. try it with property names of more than one character). The assignments are different so it doesn't simply reduce to[fancy, loop]
.
– RobG
Nov 5 at 0:54
1
@RobG: You skipped the first four layers of unpacking, each of which only takes one element."10"
gets unpacked into"1"
and"0"
, and the"0"
gets discarded in the first layer of the destructuring assignment. By the time we hit the[fancy, loop]
unpacking, the"0"
has been gone for the last three layers.
– user2357112
Nov 5 at 1:12
Yes, that's the missing part. ;-)
– RobG
Nov 5 at 1:13
add a comment |
up vote
21
down vote
accepted
up vote
21
down vote
accepted
It's not assigning 0
and 1
to [[[[[fancy, loop]]]]]
. You're looping over the keys of [0, 0]
, because you used in
instead of of
, and those keys are strings.
The string "0"
is an iterable whose sole element is "0"
. Assigning "0"
to [[[[[fancy, loop]]]]]
repeatedly unpacks "0"
and gets "0"
, until eventually it gets down to
[fancy, loop] = "0"
at which point the final unpacking assigns "0"
to fancy
and undefined
to loop
.
It's not assigning 0
and 1
to [[[[[fancy, loop]]]]]
. You're looping over the keys of [0, 0]
, because you used in
instead of of
, and those keys are strings.
The string "0"
is an iterable whose sole element is "0"
. Assigning "0"
to [[[[[fancy, loop]]]]]
repeatedly unpacks "0"
and gets "0"
, until eventually it gets down to
[fancy, loop] = "0"
at which point the final unpacking assigns "0"
to fancy
and undefined
to loop
.
answered Nov 5 at 0:39
user2357112
146k12149233
146k12149233
1
This doesn't fully explain what is occurring though, there is a difference between[fancy, loop]
and[[fancy, loop]]
given[,,,,,,,,,0,0]
(i.e. try it with property names of more than one character). The assignments are different so it doesn't simply reduce to[fancy, loop]
.
– RobG
Nov 5 at 0:54
1
@RobG: You skipped the first four layers of unpacking, each of which only takes one element."10"
gets unpacked into"1"
and"0"
, and the"0"
gets discarded in the first layer of the destructuring assignment. By the time we hit the[fancy, loop]
unpacking, the"0"
has been gone for the last three layers.
– user2357112
Nov 5 at 1:12
Yes, that's the missing part. ;-)
– RobG
Nov 5 at 1:13
add a comment |
1
This doesn't fully explain what is occurring though, there is a difference between[fancy, loop]
and[[fancy, loop]]
given[,,,,,,,,,0,0]
(i.e. try it with property names of more than one character). The assignments are different so it doesn't simply reduce to[fancy, loop]
.
– RobG
Nov 5 at 0:54
1
@RobG: You skipped the first four layers of unpacking, each of which only takes one element."10"
gets unpacked into"1"
and"0"
, and the"0"
gets discarded in the first layer of the destructuring assignment. By the time we hit the[fancy, loop]
unpacking, the"0"
has been gone for the last three layers.
– user2357112
Nov 5 at 1:12
Yes, that's the missing part. ;-)
– RobG
Nov 5 at 1:13
1
1
This doesn't fully explain what is occurring though, there is a difference between
[fancy, loop]
and [[fancy, loop]]
given [,,,,,,,,,0,0]
(i.e. try it with property names of more than one character). The assignments are different so it doesn't simply reduce to [fancy, loop]
.– RobG
Nov 5 at 0:54
This doesn't fully explain what is occurring though, there is a difference between
[fancy, loop]
and [[fancy, loop]]
given [,,,,,,,,,0,0]
(i.e. try it with property names of more than one character). The assignments are different so it doesn't simply reduce to [fancy, loop]
.– RobG
Nov 5 at 0:54
1
1
@RobG: You skipped the first four layers of unpacking, each of which only takes one element.
"10"
gets unpacked into "1"
and "0"
, and the "0"
gets discarded in the first layer of the destructuring assignment. By the time we hit the [fancy, loop]
unpacking, the "0"
has been gone for the last three layers.– user2357112
Nov 5 at 1:12
@RobG: You skipped the first four layers of unpacking, each of which only takes one element.
"10"
gets unpacked into "1"
and "0"
, and the "0"
gets discarded in the first layer of the destructuring assignment. By the time we hit the [fancy, loop]
unpacking, the "0"
has been gone for the last three layers.– user2357112
Nov 5 at 1:12
Yes, that's the missing part. ;-)
– RobG
Nov 5 at 1:13
Yes, that's the missing part. ;-)
– RobG
Nov 5 at 1:13
add a comment |
up vote
4
down vote
You're using in
instead of of
so you're getting the properties of the array not the values. For this case you're getting the array indexes as strings (0
, 1
). You're basically destructuring a string with length of 1 every time. So you always get the first character of every iterated property
add a comment |
up vote
4
down vote
You're using in
instead of of
so you're getting the properties of the array not the values. For this case you're getting the array indexes as strings (0
, 1
). You're basically destructuring a string with length of 1 every time. So you always get the first character of every iterated property
add a comment |
up vote
4
down vote
up vote
4
down vote
You're using in
instead of of
so you're getting the properties of the array not the values. For this case you're getting the array indexes as strings (0
, 1
). You're basically destructuring a string with length of 1 every time. So you always get the first character of every iterated property
You're using in
instead of of
so you're getting the properties of the array not the values. For this case you're getting the array indexes as strings (0
, 1
). You're basically destructuring a string with length of 1 every time. So you always get the first character of every iterated property
edited Nov 5 at 10:43
answered Nov 5 at 0:40
lleon
716167
716167
add a comment |
add a comment |
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
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53146810%2fhow-is-forconst-a-b-c-in-0-0-even-valid%23new-answer', 'question_page');
}
);
Post as a guest
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
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
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
@RobG Yeah, but, how could it destructure
0
and1
?– K._
Nov 5 at 0:25
2
you can always throw the code into a transpiler to see what is happening
– Bravo
Nov 5 at 0:25
2
babeljs.io/repl/…
– Bravo
Nov 5 at 0:26
17
I guess you discovered a new question for javascript job interviews. ;-)
– RobG
Nov 5 at 0:49
4
^ @RobG for anyone wondering, this should be taken as sarcasm
– Pierre Arlaud
Nov 5 at 9:28