Skip to content

Instantly share code, notes, and snippets.

@knd
Last active December 10, 2015 18:08
Show Gist options
  • Save knd/4472206 to your computer and use it in GitHub Desktop.
Save knd/4472206 to your computer and use it in GitHub Desktop.
Some code to demonstrate global and local scope in JavaScript.
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