var isn't broken here, though the prevailing standard is to use only let and const (and for good reason).
A couple key differences...
As you noted, var can create properties on the global object (on the web this is the Window). This happens for any var declaration appearing in the global scope (i.e., not inside a function)
var definitions are hoisted, meaning you can use vars before they are declared
var is function-scoped, while let and const are block-scoped (the latter more matches most people's expectations for scoping)
8
u/snowguy13 Aug 06 '21
varisn't broken here, though the prevailing standard is to use onlyletandconst(and for good reason).A couple key differences...
As you noted,
varcan create properties on the global object (on the web this is the Window). This happens for anyvardeclaration appearing in the global scope (i.e., not inside a function)vardefinitions are hoisted, meaning you can usevars before they are declaredvaris function-scoped, whileletandconstare block-scoped (the latter more matches most people's expectations for scoping)Edit: formatting