Skip to content

Instantly share code, notes, and snippets.

@lorepirri
Created July 18, 2017 20:14
Show Gist options
  • Save lorepirri/8a80b02fd726f7b1ed782a862fde2a1b to your computer and use it in GitHub Desktop.
Save lorepirri/8a80b02fd726f7b1ed782a862fde2a1b to your computer and use it in GitHub Desktop.
In Javascript scopes are defined at function level, if declared with var, on the other side es6 let/const define scope at a block level (e.g. in an if-block, or for loop)
console.clear();
if (true) {
let insideJoke = 174; // let used -> block's scope
const anotherInsideJoke = 100; // const used -> block's scope
var thisIsACommonJoke = 42 // var used -> scope to outer scope (global)
}
console.log('thisIsACommonJoke', thisIsACommonJoke); // ok, global scope
console.log('insideJoke', insideJoke); // error: out of scope
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment