Last Updated: February 25, 2016
·
423
· 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');
}

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Related Tags
#javascript
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside