Skip to content

Instantly share code, notes, and snippets.

@fortunee
Last active November 6, 2018 15:00
Show Gist options
  • Save fortunee/f542794fe68140819c864149f228cf1f to your computer and use it in GitHub Desktop.
Save fortunee/f542794fe68140819c864149f228cf1f to your computer and use it in GitHub Desktop.
Local (Function-level) Scope
let name = 'Fortune';
function func() {
let age = 22;
console.log(name, age);
}
func() // 'Fortune 22'
// Accessing age here throws
console.log(age) // Uncaught ReferenceError: age is not defined
/**
* Variable declared without the var, let or const keyword
* gets thrown into the function's outer scope
*/
function withoutVarLetOrConst() {
out = 'Acessible within the outer scope';
}
withoutVarLetOrConst();
console.log(out); // -> 'Accessible within the outer scope'
// Variable "name" can be changed within a function scope
function changeName() {
name = 'Rose';
}
// Calling the function modifies the global variable "name"
changeName();
console.log(name) // -> 'Rose'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment