JavaScript variable and function hoisting



thedotisblack


Hoisting is just another bit of JavaScript that can leave some people perplexed. Let’s sort that out.

There are mainly two cases of hoisting: variable and function hoisting.

Variable hoisting (regular object)

JavaScript scope is function based so when you declare a local variable using the keyword var it creates an implicit initialisation on the top setting the variable to undefined.

Variable hoisting (reference to a function)

The same behaviour happens when the variable references a function.

Function hoisting

Function hoisting works completely different. JavaScript makes available all the functions at the top regardless when they are defined.