Created
December 25, 2012 11:15
-
-
Save mrosati84/4372723 to your computer and use it in GitHub Desktop.
this gist explains function hoisting in javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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