Skip to content

Instantly share code, notes, and snippets.

@MaraAlexa
Created May 5, 2017 11:43
Show Gist options
  • Save MaraAlexa/70d907e509305de408a0b9d53bffee29 to your computer and use it in GitHub Desktop.
Save MaraAlexa/70d907e509305de408a0b9d53bffee29 to your computer and use it in GitHub Desktop.
ES6 : var vs. let and const
// BAD - the dogYear variable leaks out to the window scope
var age = 100;
if(age > 12) {
var dogYears = age * 7;
console.log(`You are ${dogYears} dog years old!`);
}
console.log(dogYears); // 700
// GOOD - the dogYears variable stays available only to the block
var age = 100;
if(age > 12) {
let dogYears = age * 7;
console.log(`You are ${dogYears} dog years old!`);
}
console.log(dogYears); // dogYears is NOT DEFINED bc let is available only to the block
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment