How is “for(const [[[[[a, b, c]]]]] in [0, 0]);” even valid?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
7
down vote

favorite
1












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?










share|improve this question























  • @RobG Yeah, but, how could it destructure 0 and 1?
    – 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














up vote
7
down vote

favorite
1












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?










share|improve this question























  • @RobG Yeah, but, how could it destructure 0 and 1?
    – 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












up vote
7
down vote

favorite
1









up vote
7
down vote

favorite
1






1





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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 52 mins ago

























asked 55 mins ago









K._

2,21221434




2,21221434











  • @RobG Yeah, but, how could it destructure 0 and 1?
    – 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






  • 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












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.






share|improve this answer
















  • 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

















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






share|improve this answer




















    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',
    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
    );



    );













     

    draft saved


    draft discarded


















    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






























    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.






    share|improve this answer
















    • 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














    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.






    share|improve this answer
















    • 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












    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.






    share|improve this answer












    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.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    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












    • 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












    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






    share|improve this answer
























      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






      share|improve this answer






















        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






        share|improve this answer












        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







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 34 mins ago









        lleon

        696167




        696167



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            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













































































            Comments

            Popular posts from this blog

            What does second last employer means? [closed]

            Installing NextGIS Connect into QGIS 3?

            One-line joke