It's not a good reason. But 5 it's the one we have.
Good reasons? Honestly can't think of one, it's 11 one of the few things I really dislike about 10 JS.
It's possible because everything happens 9 within the global scope if not otherwise 8 controlled and JS allows implicit variable 7 creation like this. The cost of this is 6 enormous potential for scoping bugs and 5 pollution, and only benefit given that "this" exists 4 to explicitly define self scope and "window" and 3 "document" exist for global referencing, is 2 saving a few characters - which is no reason 1 at all.
var keyword 3 is more responsible for scope of a variable 2 than its declaration.
And I don't think that 1 most programming languages have a check on that.
Lua has a similar issue: any non-declared variable 7 is global by default. In the mailing list 6 it's a recurring theme, asking why shouldn't 5 it be 'local by default'. unfortunately, that 4 would introduce very nasty ambiguities in 3 the language, so the conclusion typically 2 is "global by default is bad, local 1 by default is worse"
Because it is a scripting language. Fact 1 kids. And that is how it was designed!
More Related questions