Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@micahlmartin
Last active December 26, 2015 16:49
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 micahlmartin/d5fb495a181143faea12 to your computer and use it in GitHub Desktop.
Save micahlmartin/d5fb495a181143faea12 to your computer and use it in GitHub Desktop.
Global namespace example
(function() {
global_var = "This is a globally scoped variable";
console.log(global_var);
return {
data: "example"
}
})()
console.log(global_var);
function global_vars() {
global_var = "This is a globally scoped variable";
console.log(global_var);
}
global_vars();
// the variable "local" exists in the global namespace
// because it was created without the `var` keyword
console.log(global_var);
function local_vars() {
var local = "This is scoped to this function only";
console.log(local);
}
local_vars();
// The variable "local" is undefined because we are in the global scope
console.log(typeof local === 'undefined')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment