Wednesday, March 23, 2016

What is JavaScript hoisting?

In programming, usually a variable has to be declared before it is used. However with JavaScript you can declare a variable after it is used. Hoisting is JavaScript's default behaviour. JavaScript hoisting moves up all declarations to the top. Note that variable declarations are moved up and not the variable assignments.

This is variable declaration: This gets moved up to top
var x;

This is variable assignment:This does not move up, stays put where defined.
y=10;

No comments: