Last active
December 26, 2015 16:49
-
-
Save micahlmartin/d5fb495a181143faea12 to your computer and use it in GitHub Desktop.
Global namespace example
This file contains hidden or 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
(function() { | |
global_var = "This is a globally scoped variable"; | |
console.log(global_var); | |
return { | |
data: "example" | |
} | |
})() | |
console.log(global_var); |
This file contains hidden or 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
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); |
This file contains hidden or 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
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