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
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
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#