Why JavaScript doesn't warn me when I use arr.lenght (misspelt) instead of arr.length in a loop? I also use...











up vote
7
down vote

favorite












I spent hours just to find out that I misspelt the word .length with .lenght. It can run normally with no warning at all. Why...?



I use 'use strict' and run on Node 10.13.0.



Code:






'use strict';
let arr = [1, 2, 3, 4];
for(let i = 0; i < arr.lenght; i++) {
console.log(arr[i]);
}












share|improve this question




















  • 1




    You could easily add new properties to arr object, JavaScript won't warn you about it, instead it will try to find the property you're calling, and if it didn't find anything such result will be undefined, so the comparison is actually i < undefined everytime. I'll suggest you to read stackoverflow.com/questions/1335851/….
    – mmontoya
    51 mins ago










  • Simple answer is...because it's a loosely typed language. Everyone has made property name typos. Lick your wounds and move on
    – charlietfl
    44 mins ago

















up vote
7
down vote

favorite












I spent hours just to find out that I misspelt the word .length with .lenght. It can run normally with no warning at all. Why...?



I use 'use strict' and run on Node 10.13.0.



Code:






'use strict';
let arr = [1, 2, 3, 4];
for(let i = 0; i < arr.lenght; i++) {
console.log(arr[i]);
}












share|improve this question




















  • 1




    You could easily add new properties to arr object, JavaScript won't warn you about it, instead it will try to find the property you're calling, and if it didn't find anything such result will be undefined, so the comparison is actually i < undefined everytime. I'll suggest you to read stackoverflow.com/questions/1335851/….
    – mmontoya
    51 mins ago










  • Simple answer is...because it's a loosely typed language. Everyone has made property name typos. Lick your wounds and move on
    – charlietfl
    44 mins ago















up vote
7
down vote

favorite









up vote
7
down vote

favorite











I spent hours just to find out that I misspelt the word .length with .lenght. It can run normally with no warning at all. Why...?



I use 'use strict' and run on Node 10.13.0.



Code:






'use strict';
let arr = [1, 2, 3, 4];
for(let i = 0; i < arr.lenght; i++) {
console.log(arr[i]);
}












share|improve this question















I spent hours just to find out that I misspelt the word .length with .lenght. It can run normally with no warning at all. Why...?



I use 'use strict' and run on Node 10.13.0.



Code:






'use strict';
let arr = [1, 2, 3, 4];
for(let i = 0; i < arr.lenght; i++) {
console.log(arr[i]);
}








'use strict';
let arr = [1, 2, 3, 4];
for(let i = 0; i < arr.lenght; i++) {
console.log(arr[i]);
}





'use strict';
let arr = [1, 2, 3, 4];
for(let i = 0; i < arr.lenght; i++) {
console.log(arr[i]);
}






javascript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 38 mins ago









CertainPerformance

68.7k143453




68.7k143453










asked 56 mins ago









MangoLato

383




383








  • 1




    You could easily add new properties to arr object, JavaScript won't warn you about it, instead it will try to find the property you're calling, and if it didn't find anything such result will be undefined, so the comparison is actually i < undefined everytime. I'll suggest you to read stackoverflow.com/questions/1335851/….
    – mmontoya
    51 mins ago










  • Simple answer is...because it's a loosely typed language. Everyone has made property name typos. Lick your wounds and move on
    – charlietfl
    44 mins ago
















  • 1




    You could easily add new properties to arr object, JavaScript won't warn you about it, instead it will try to find the property you're calling, and if it didn't find anything such result will be undefined, so the comparison is actually i < undefined everytime. I'll suggest you to read stackoverflow.com/questions/1335851/….
    – mmontoya
    51 mins ago










  • Simple answer is...because it's a loosely typed language. Everyone has made property name typos. Lick your wounds and move on
    – charlietfl
    44 mins ago










1




1




You could easily add new properties to arr object, JavaScript won't warn you about it, instead it will try to find the property you're calling, and if it didn't find anything such result will be undefined, so the comparison is actually i < undefined everytime. I'll suggest you to read stackoverflow.com/questions/1335851/….
– mmontoya
51 mins ago




You could easily add new properties to arr object, JavaScript won't warn you about it, instead it will try to find the property you're calling, and if it didn't find anything such result will be undefined, so the comparison is actually i < undefined everytime. I'll suggest you to read stackoverflow.com/questions/1335851/….
– mmontoya
51 mins ago












Simple answer is...because it's a loosely typed language. Everyone has made property name typos. Lick your wounds and move on
– charlietfl
44 mins ago






Simple answer is...because it's a loosely typed language. Everyone has made property name typos. Lick your wounds and move on
– charlietfl
44 mins ago














4 Answers
4






active

oldest

votes

















up vote
8
down vote



accepted










Because when you try to get a property that does'nt exist, it returns undefined, and 0 < undefined is false.
Take into account that javascript is not a strongly typed language. You can add new properties by simply giving it a value arr.something=123, even you can set arr.length=7 but it's not a good idea.






let arr = [1, 2, 3, 4];
console.log(arr.lenght) // undefined
console.log(arr.qwerty) // undefined
console.log(arr.lenght < 9999) // false
console.log(arr.lenght > 9999) // false

arr.length = 7 // <-- it's not a good idea
for(let i = 0; i < arr.length; i++) {console.log(arr[i])}








share|improve this answer























  • That makes sense, thanks.
    – MangoLato
    45 mins ago


















up vote
2
down vote













The the upper bound of the loop is specified as lenght, a typo for the local variable length. At runtime, lenght will evaluate to undefined, so the check i < lenght will always fail, and the loop body is never executed.






share|improve this answer




























    up vote
    1
    down vote













    Javascript arrays are treated as objects (though they are instances of Array). Hence, when you write arr.lenght, it treats lenght as a property of an object that is undefined. Hence, you don't get an error. It simply tries to get a property that is undefined. Also, in your case, the loop just does not execute as the condition of the loop is never satisfied.






    share|improve this answer




























      up vote
      1
      down vote













      You could easily add new properties to arr object, JavaScript won't warn you about it, instead it will try to find the property you're calling, and if it didn't find anything such result will be undefined, so the comparison is actually i < undefined everytime because you're calling a property that haven't been created on the object. I'll suggest you to read What does "use strict" do in JavaScript, and what is the reasoning behind it?.






      share|improve this answer








      New contributor




      mmontoya is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.


















        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%2f53586954%2fwhy-javascript-doesnt-warn-me-when-i-use-arr-lenght-misspelt-instead-of-arr-l%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        8
        down vote



        accepted










        Because when you try to get a property that does'nt exist, it returns undefined, and 0 < undefined is false.
        Take into account that javascript is not a strongly typed language. You can add new properties by simply giving it a value arr.something=123, even you can set arr.length=7 but it's not a good idea.






        let arr = [1, 2, 3, 4];
        console.log(arr.lenght) // undefined
        console.log(arr.qwerty) // undefined
        console.log(arr.lenght < 9999) // false
        console.log(arr.lenght > 9999) // false

        arr.length = 7 // <-- it's not a good idea
        for(let i = 0; i < arr.length; i++) {console.log(arr[i])}








        share|improve this answer























        • That makes sense, thanks.
          – MangoLato
          45 mins ago















        up vote
        8
        down vote



        accepted










        Because when you try to get a property that does'nt exist, it returns undefined, and 0 < undefined is false.
        Take into account that javascript is not a strongly typed language. You can add new properties by simply giving it a value arr.something=123, even you can set arr.length=7 but it's not a good idea.






        let arr = [1, 2, 3, 4];
        console.log(arr.lenght) // undefined
        console.log(arr.qwerty) // undefined
        console.log(arr.lenght < 9999) // false
        console.log(arr.lenght > 9999) // false

        arr.length = 7 // <-- it's not a good idea
        for(let i = 0; i < arr.length; i++) {console.log(arr[i])}








        share|improve this answer























        • That makes sense, thanks.
          – MangoLato
          45 mins ago













        up vote
        8
        down vote



        accepted







        up vote
        8
        down vote



        accepted






        Because when you try to get a property that does'nt exist, it returns undefined, and 0 < undefined is false.
        Take into account that javascript is not a strongly typed language. You can add new properties by simply giving it a value arr.something=123, even you can set arr.length=7 but it's not a good idea.






        let arr = [1, 2, 3, 4];
        console.log(arr.lenght) // undefined
        console.log(arr.qwerty) // undefined
        console.log(arr.lenght < 9999) // false
        console.log(arr.lenght > 9999) // false

        arr.length = 7 // <-- it's not a good idea
        for(let i = 0; i < arr.length; i++) {console.log(arr[i])}








        share|improve this answer














        Because when you try to get a property that does'nt exist, it returns undefined, and 0 < undefined is false.
        Take into account that javascript is not a strongly typed language. You can add new properties by simply giving it a value arr.something=123, even you can set arr.length=7 but it's not a good idea.






        let arr = [1, 2, 3, 4];
        console.log(arr.lenght) // undefined
        console.log(arr.qwerty) // undefined
        console.log(arr.lenght < 9999) // false
        console.log(arr.lenght > 9999) // false

        arr.length = 7 // <-- it's not a good idea
        for(let i = 0; i < arr.length; i++) {console.log(arr[i])}








        let arr = [1, 2, 3, 4];
        console.log(arr.lenght) // undefined
        console.log(arr.qwerty) // undefined
        console.log(arr.lenght < 9999) // false
        console.log(arr.lenght > 9999) // false

        arr.length = 7 // <-- it's not a good idea
        for(let i = 0; i < arr.length; i++) {console.log(arr[i])}





        let arr = [1, 2, 3, 4];
        console.log(arr.lenght) // undefined
        console.log(arr.qwerty) // undefined
        console.log(arr.lenght < 9999) // false
        console.log(arr.lenght > 9999) // false

        arr.length = 7 // <-- it's not a good idea
        for(let i = 0; i < arr.length; i++) {console.log(arr[i])}






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 11 mins ago

























        answered 53 mins ago









        eag845

        38229




        38229












        • That makes sense, thanks.
          – MangoLato
          45 mins ago


















        • That makes sense, thanks.
          – MangoLato
          45 mins ago
















        That makes sense, thanks.
        – MangoLato
        45 mins ago




        That makes sense, thanks.
        – MangoLato
        45 mins ago












        up vote
        2
        down vote













        The the upper bound of the loop is specified as lenght, a typo for the local variable length. At runtime, lenght will evaluate to undefined, so the check i < lenght will always fail, and the loop body is never executed.






        share|improve this answer

























          up vote
          2
          down vote













          The the upper bound of the loop is specified as lenght, a typo for the local variable length. At runtime, lenght will evaluate to undefined, so the check i < lenght will always fail, and the loop body is never executed.






          share|improve this answer























            up vote
            2
            down vote










            up vote
            2
            down vote









            The the upper bound of the loop is specified as lenght, a typo for the local variable length. At runtime, lenght will evaluate to undefined, so the check i < lenght will always fail, and the loop body is never executed.






            share|improve this answer












            The the upper bound of the loop is specified as lenght, a typo for the local variable length. At runtime, lenght will evaluate to undefined, so the check i < lenght will always fail, and the loop body is never executed.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 52 mins ago









            fuzz

            15k17108183




            15k17108183






















                up vote
                1
                down vote













                Javascript arrays are treated as objects (though they are instances of Array). Hence, when you write arr.lenght, it treats lenght as a property of an object that is undefined. Hence, you don't get an error. It simply tries to get a property that is undefined. Also, in your case, the loop just does not execute as the condition of the loop is never satisfied.






                share|improve this answer

























                  up vote
                  1
                  down vote













                  Javascript arrays are treated as objects (though they are instances of Array). Hence, when you write arr.lenght, it treats lenght as a property of an object that is undefined. Hence, you don't get an error. It simply tries to get a property that is undefined. Also, in your case, the loop just does not execute as the condition of the loop is never satisfied.






                  share|improve this answer























                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    Javascript arrays are treated as objects (though they are instances of Array). Hence, when you write arr.lenght, it treats lenght as a property of an object that is undefined. Hence, you don't get an error. It simply tries to get a property that is undefined. Also, in your case, the loop just does not execute as the condition of the loop is never satisfied.






                    share|improve this answer












                    Javascript arrays are treated as objects (though they are instances of Array). Hence, when you write arr.lenght, it treats lenght as a property of an object that is undefined. Hence, you don't get an error. It simply tries to get a property that is undefined. Also, in your case, the loop just does not execute as the condition of the loop is never satisfied.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 50 mins ago









                    Rohan Dhar

                    624213




                    624213






















                        up vote
                        1
                        down vote













                        You could easily add new properties to arr object, JavaScript won't warn you about it, instead it will try to find the property you're calling, and if it didn't find anything such result will be undefined, so the comparison is actually i < undefined everytime because you're calling a property that haven't been created on the object. I'll suggest you to read What does "use strict" do in JavaScript, and what is the reasoning behind it?.






                        share|improve this answer








                        New contributor




                        mmontoya is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                        Check out our Code of Conduct.






















                          up vote
                          1
                          down vote













                          You could easily add new properties to arr object, JavaScript won't warn you about it, instead it will try to find the property you're calling, and if it didn't find anything such result will be undefined, so the comparison is actually i < undefined everytime because you're calling a property that haven't been created on the object. I'll suggest you to read What does "use strict" do in JavaScript, and what is the reasoning behind it?.






                          share|improve this answer








                          New contributor




                          mmontoya is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.




















                            up vote
                            1
                            down vote










                            up vote
                            1
                            down vote









                            You could easily add new properties to arr object, JavaScript won't warn you about it, instead it will try to find the property you're calling, and if it didn't find anything such result will be undefined, so the comparison is actually i < undefined everytime because you're calling a property that haven't been created on the object. I'll suggest you to read What does "use strict" do in JavaScript, and what is the reasoning behind it?.






                            share|improve this answer








                            New contributor




                            mmontoya is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.









                            You could easily add new properties to arr object, JavaScript won't warn you about it, instead it will try to find the property you're calling, and if it didn't find anything such result will be undefined, so the comparison is actually i < undefined everytime because you're calling a property that haven't been created on the object. I'll suggest you to read What does "use strict" do in JavaScript, and what is the reasoning behind it?.







                            share|improve this answer








                            New contributor




                            mmontoya is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.









                            share|improve this answer



                            share|improve this answer






                            New contributor




                            mmontoya is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.









                            answered 45 mins ago









                            mmontoya

                            1138




                            1138




                            New contributor




                            mmontoya is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.





                            New contributor





                            mmontoya is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.






                            mmontoya is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.





                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                Please pay close attention to the following guidance:


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53586954%2fwhy-javascript-doesnt-warn-me-when-i-use-arr-lenght-misspelt-instead-of-arr-l%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                What visual should I use to simply compare current year value vs last year in Power BI desktop

                                Alexandru Averescu

                                Trompette piccolo