Hoisting

Hoisting is a term you will not find used in any normative specification prose prior to ECMAScript® 2015 Language Specification.

Conceptually, for example, a strict definition of hoisting suggests that variable and function declarations are physically moved to the top of your code. In JavaScript, Hoisting is the default behavior of moving all the declarations at the top of the scope before code execution.

Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their scope before code execution. This means that the declarations will be moved to the top of their scope but not their assignations, their assignations will remain in the same spot.

Initializations are not hoisted!

Hoisting only works for declarations, not initializations. So,

console.log(y);
var y = 13;

The above code will give undefined as output, as it is the same as writing,

var y;
console.log(y);
y = 13;

Hoisting example:

// JavaScript Hoisting Example
x = 5; // Assign 5 to x
console.log(x);
var x; // Declare x

Last updated