Hacker News new | ask | show | jobs
by amirhhz 5264 days ago
In JavaScript, if you check for a non-existent property on a variable (e.g. aVar.lenght vs aVar.length) it will return "undefined". So people often rely on this behaviour to check if something is an array or not (no comment on whether this is good or bad), with:

    if(somethingThatMightBeAnArray.length){
        // do things with array
    }
So misspelling of length can be making a lot of code out there behave in an unexpected way.
2 comments

The same pattern is widely used to test whether an array-like object is empty. Since a length of 0 is also "falsey", it evaluates as false when the array has no elements. A typo in this case would result in the tested array always being "empty".
That is a bad thing as String also have a length property

"bar".length; // 3