Skip to content

Instantly share code, notes, and snippets.

@finalfantasia
Last active August 29, 2015 14:03
Show Gist options
  • Save finalfantasia/d03daaf7e084d1f3a2a7 to your computer and use it in GitHub Desktop.
Save finalfantasia/d03daaf7e084d1f3a2a7 to your computer and use it in GitHub Desktop.
JavaScript Scoping and Hoisting
// related write-up
// http://www.adequatelygood.com/JavaScript-Scoping-and-Hoisting.html
// example code
var a = 1;
function b (arguments) {
var a = 3;
console.log (a);
console.log (arguments);
return;
function a () {}
}
b (2);
console.log (a);
// output
/*
3
2
1
*/
// what the code looks like after "hoisting"
function b (arguments) {
function a () {}
var a; // no effect, this declartation doesn't override an existing property.
a = 3; // the function is gone at this point.
console.log (a);
console.log (arguments);
return;
}
var a;
a = 1;
b (2);
console.log (a);
// output
/*
3
2
1
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment