Skip to content

Instantly share code, notes, and snippets.

@harrisonmalone
Last active November 21, 2018 11:04
Show Gist options
  • Save harrisonmalone/0b177232892774d5b6221d7e4f96c94f to your computer and use it in GitHub Desktop.
Save harrisonmalone/0b177232892774d5b6221d7e4f96c94f to your computer and use it in GitHub Desktop.
excellent test for scope and the way javascript works
function getDemo(demo) {
const test = function() {
return demo + ' my friend'
}
// an anonymous function is assigned to a variable test
demo = demo + ' how are you'
// we do some string concatenation
// => "hi how are you"
return test()
// we return test() which invokes the function, as javascript goes from inner to outer scope demo within this function is "hello how are you" and it's concatenated with " my friend", this value is returned
}
console.log(getDemo("hi"))
// function is called
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment