Skip to content

Instantly share code, notes, and snippets.

@mrosati84
Created December 25, 2012 11:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mrosati84/4372723 to your computer and use it in GitHub Desktop.
Save mrosati84/4372723 to your computer and use it in GitHub Desktop.
this gist explains function hoisting in javascript
(function () {
function foo() {
console.log('foo');
}
function bar() {
console.log('bar');
}
function hoistMe() {
console.log(typeof foo); // outputs "function"
console.log(typeof bar); // outputs "undefined"
foo(); // runs normally
bar(); // gives an error (calling undefined)
// this one is hoisted: typeof and function call work correctly
function foo() {
console.log('local foo');
}
// this one is not hoisted!
var bar = function() {
console.log('local bar');
}
}
hoistMe();
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment