Javascript has two kinds of scope:
- Local (inside a function)
- Global (outside a function)
- Using the
var
keywords creates the variable in the current scope- If the current scope is global, then
var
is 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