Last active
December 10, 2015 18:08
-
-
Save knd/4472206 to your computer and use it in GitHub Desktop.
Some code to demonstrate global and local scope in JavaScript.
This file contains 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
ramsay = "chef"; // global variable 'ramsay' | |
function hellKitchen() { | |
knd = "chef"; // global variable 'knd' | |
} | |
function hellKitchen101() { | |
var chuckNorris = "chef"; // local variable 'chuckNorris' to this function | |
// watch out! this function 'assistant()' is locally scoped too | |
// can't call it outside of hellKitchen101 | |
function assistant() { | |
console.log("I'm assistant"); | |
} | |
} | |
function myKitchen() { | |
console.log(ramsay); // of course 'ramsay' is "chef" | |
console.log(knd); // 'knd' is "chef" ! :p | |
console.log(chuckNorris); // error raised, 'chuckNorris' is not defined ! | |
} | |
hellKitchen(); | |
hellKitchen1010(); | |
myKitchen(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment