Javascript has two kinds of scope:
- Local (inside a function)
- Global (outside a function)
- Using the
varkeywords creates the variable in the current scope- If the current scope is global, then
varis unnecessary (see below) - If the current scope is local, then you’re creating a local variable in the current scope
- If the current scope is global, then
- If you don’t use
var, then Javascript goes up the “scope chain” to see if it’s already been declared