Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dannycallaghan/6822545 to your computer and use it in GitHub Desktop.
Save dannycallaghan/6822545 to your computer and use it in GitHub Desktop.
Hoisting - function declarations vs function expressions
/* 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