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');
}
Written by Ron Apelbaum
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Javascript
Authors
Related Tags
#javascript
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#