Hoisting
is the concept in which Javascript, by default, moves all declarations to the top of the current scope. As such, a variable can be used before it has been declared.
Note that Javascript
only hoists declarations and not initializations.
Source
There are two types of hoisting:
- variable hoisting - rare
- function hoisting - more common

Wherever a var
(or function declaration) appears inside a scope, that declaration is taken to belong to the entire scope and accessible everywhere throughout.
xxxxxxxxxx
12
var a = 2;
foo(); // works because `foo()`
// declaration is "hoisted"
function foo() {
a = 3;
console.log( a ); // 3
var a; // declaration is "hoisted"
// to the top of `foo()`
}
console.log( a ); // 2
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment