How is âÂÂfor(const [[[[[a, b, c]]]]] in [0, 0]);â even valid?
Clash Royale CLAN TAG#URR8PPP
up vote
7
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
7
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._
49 mins ago
1
you can always throw the code into a transpiler to see what is happening
â Bravo
49 mins ago
1
babeljs.io/repl/â¦
â Bravo
49 mins ago
1
I guess you discovered a new question for javascript job interviews. ;-)
â RobG
26 mins ago
add a comment |Â
up vote
7
down vote
favorite
up vote
7
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 52 mins ago
asked 55 mins ago
K._
2,21221434
2,21221434
@RobG Yeah, but, how could it destructure0
and1
?
â K._
49 mins ago
1
you can always throw the code into a transpiler to see what is happening
â Bravo
49 mins ago
1
babeljs.io/repl/â¦
â Bravo
49 mins ago
1
I guess you discovered a new question for javascript job interviews. ;-)
â RobG
26 mins ago
add a comment |Â
@RobG Yeah, but, how could it destructure0
and1
?
â K._
49 mins ago
1
you can always throw the code into a transpiler to see what is happening
â Bravo
49 mins ago
1
babeljs.io/repl/â¦
â Bravo
49 mins ago
1
I guess you discovered a new question for javascript job interviews. ;-)
â RobG
26 mins ago
@RobG Yeah, but, how could it destructure
0
and 1
?â K._
49 mins ago
@RobG Yeah, but, how could it destructure
0
and 1
?â K._
49 mins ago
1
1
you can always throw the code into a transpiler to see what is happening
â Bravo
49 mins ago
you can always throw the code into a transpiler to see what is happening
â Bravo
49 mins ago
1
1
babeljs.io/repl/â¦
â Bravo
49 mins ago
babeljs.io/repl/â¦
â Bravo
49 mins ago
1
1
I guess you discovered a new question for javascript job interviews. ;-)
â RobG
26 mins ago
I guess you discovered a new question for javascript job interviews. ;-)
â RobG
26 mins ago
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
5
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
20 mins ago
@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
2 mins ago
Yes, that's the missing part. ;-)
â RobG
2 mins ago
add a comment |Â
up vote
2
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. You're basically destructuring a string with length of 1 every time. So your 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
5
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
20 mins ago
@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
2 mins ago
Yes, that's the missing part. ;-)
â RobG
2 mins ago
add a comment |Â
up vote
5
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
20 mins ago
@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
2 mins ago
Yes, that's the missing part. ;-)
â RobG
2 mins ago
add a comment |Â
up vote
5
down vote
accepted
up vote
5
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 36 mins ago
user2357112
145k12147230
145k12147230
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
20 mins ago
@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
2 mins ago
Yes, that's the missing part. ;-)
â RobG
2 mins ago
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
20 mins ago
@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
2 mins ago
Yes, that's the missing part. ;-)
â RobG
2 mins ago
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
20 mins ago
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
20 mins ago
@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
2 mins ago
@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
2 mins ago
Yes, that's the missing part. ;-)
â RobG
2 mins ago
Yes, that's the missing part. ;-)
â RobG
2 mins ago
add a comment |Â
up vote
2
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. You're basically destructuring a string with length of 1 every time. So your always get the first character of every iterated property
add a comment |Â
up vote
2
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. You're basically destructuring a string with length of 1 every time. So your always get the first character of every iterated property
add a comment |Â
up vote
2
down vote
up vote
2
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. You're basically destructuring a string with length of 1 every time. So your 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. You're basically destructuring a string with length of 1 every time. So your always get the first character of every iterated property
answered 34 mins ago
lleon
696167
696167
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._
49 mins ago
1
you can always throw the code into a transpiler to see what is happening
â Bravo
49 mins ago
1
babeljs.io/repl/â¦
â Bravo
49 mins ago
1
I guess you discovered a new question for javascript job interviews. ;-)
â RobG
26 mins ago