Skip to content

Instantly share code, notes, and snippets.

@tekaratzas
Last active July 21, 2017 18:46
Show Gist options
  • Save tekaratzas/e7e2e01769ca72c2a692fd470f07b110 to your computer and use it in GitHub Desktop.
Save tekaratzas/e7e2e01769ca72c2a692fd470f07b110 to your computer and use it in GitHub Desktop.
Exploring Scopes And namespaces in JavaScript
var foo = 12;
function changeFoo() {
foo = 34; // changes global scope and not local scope!
}
changeFoo();
console.log(foo);
// This becomes a more apparent issue in next example
// Out here is the global scope
for(var i = 0; i < 10; i++) {
innerLoop();
}
function innerLoop() {
// this is a different scope
for(i = 0; i < 10; i++) { // missing var statement! i refers to global scope!
console.log("this is will show 10 times and not 100.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment