Last active
November 6, 2018 15:00
-
-
Save fortunee/f542794fe68140819c864149f228cf1f to your computer and use it in GitHub Desktop.
Local (Function-level) Scope
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
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