Last Updated: February 25, 2016
·
222
· apelbaur

javascript hoisting

var declaration

hoisted to the top of scope, without assignment.

var a = 1;
function printA() {
    console.log(a);
    var a = 2;
    console.log(a);
}
printA();
//undefined
//2

function declaration

hoisted with assignment.

foo();
//foo
function foo() {
    console.log('foo');
}

Pay attention!

while function declarations are hoisted with their assignment, function expressions are just regular vars hence hoisted without their assignment.

goo();
//undefined is not a function
var goo = function () {
    console.log('goo');
}