JavaScript variable and function hoisting

Gerard Sans
Sons of JavaScript
Published in
1 min readDec 13, 2014
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.

--

--

Gerard Sans
Sons of JavaScript

Helping Devs to succeed #AI #web3 / ex @AWSCloud / Just be AWSome / MC Speaker Trainer Community Leader @web3_london / @ReactEurope @ReactiveConf @ngcruise