Created
October 4, 2013 08:06
-
-
Save dannycallaghan/6822545 to your computer and use it in GitHub Desktop.
Hoisting - function declarations vs function expressions
This file contains 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
/* Hoisting - function declarations vs function expressions */ | |
function foo () { alert( 'global foo' ); } | |
function bar () { alert( 'global bar' ); } | |
function hoistMe () { | |
console.log( typeof foo ); // "function" | |
console.log( typeof bar ); // "undefined" | |
foo(); // alerts "local foo" | |
bar(); // throws TypeError: bar is not a function | |
// ## function declaration ##: | |
// variable 'foo' AND its implementation get hoisted | |
function foo () { alert( 'local foo' ); } | |
// ## function expression ##: | |
// only variable 'bar' gets hoisted, NOT the implementation | |
var bar = function () { alert('local bar'); }; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment