javascript allows the use of variables that have not yet been declared and allows variables to be declared after they were first used. For example, the following syntax is legal:

 
x = 123;
 
console.log("number is ", 123);
 
var x;

Hoisting is a behaviour where the javascript engine will move declarations to the top of the current scope so the above code would be interpreted as:

var x;
x = 123;
console.log("number is ", 123);

Function Hoisting

Function definitions/declarations are also hoisted such that they can be called before they have been defined as in the following example:

foo();
 
function foo() {
	alert("Hello!");
}

However,

Reference