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











up vote
22
down vote

favorite
4












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._
    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

















up vote
22
down vote

favorite
4












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._
    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















up vote
22
down vote

favorite
4









up vote
22
down vote

favorite
4






4





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 Nov 5 at 0:23

























asked Nov 5 at 0:20









K._

2,29521436




2,29521436












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






  • 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














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.






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
    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


















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






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
    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.






    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
      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















    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.






    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
      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













    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.






    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 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














    • 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












    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






    share|improve this answer



























      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






      share|improve this answer

























        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






        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 (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







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 5 at 10:43

























        answered Nov 5 at 0:40









        lleon

        716167




        716167






























             

            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




















































































            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini