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

  • A+

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.


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


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  for(let i = 0; i < arr.length; i++) {console.log(arr[i])}


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: