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._
    45 mins ago






  • 1




    you can always throw the code into a transpiler to see what is happening
    – Bravo
    44 mins ago







  • 1




    babeljs.io/repl/…
    – Bravo
    44 mins ago






  • 1




    I guess you discovered a new question for javascript job interviews. ;-)
    – RobG
    21 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._
    45 mins ago






  • 1




    you can always throw the code into a transpiler to see what is happening
    – Bravo
    44 mins ago







  • 1




    babeljs.io/repl/…
    – Bravo
    44 mins ago






  • 1




    I guess you discovered a new question for javascript job interviews. ;-)
    – RobG
    21 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 47 mins ago

























asked 50 mins ago









K._

2,21221434




2,21221434











  • @RobG Yeah, but, how could it destructure 0 and 1?
    – K._
    45 mins ago






  • 1




    you can always throw the code into a transpiler to see what is happening
    – Bravo
    44 mins ago







  • 1




    babeljs.io/repl/…
    – Bravo
    44 mins ago






  • 1




    I guess you discovered a new question for javascript job interviews. ;-)
    – RobG
    21 mins ago
















  • @RobG Yeah, but, how could it destructure 0 and 1?
    – K._
    45 mins ago






  • 1




    you can always throw the code into a transpiler to see what is happening
    – Bravo
    44 mins ago







  • 1




    babeljs.io/repl/…
    – Bravo
    44 mins ago






  • 1




    I guess you discovered a new question for javascript job interviews. ;-)
    – RobG
    21 mins ago















@RobG Yeah, but, how could it destructure 0 and 1?
– K._
45 mins ago




@RobG Yeah, but, how could it destructure 0 and 1?
– K._
45 mins ago




1




1




you can always throw the code into a transpiler to see what is happening
– Bravo
44 mins ago





you can always throw the code into a transpiler to see what is happening
– Bravo
44 mins ago





1




1




babeljs.io/repl/…
– Bravo
44 mins ago




babeljs.io/repl/…
– Bravo
44 mins ago




1




1




I guess you discovered a new question for javascript job interviews. ;-)
– RobG
21 mins ago




I guess you discovered a new question for javascript job interviews. ;-)
– RobG
21 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
    16 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
      16 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
      16 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 31 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
      16 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
      16 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
    16 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
    16 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 29 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













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay