Created
May 5, 2017 11:43
-
-
Save MaraAlexa/70d907e509305de408a0b9d53bffee29 to your computer and use it in GitHub Desktop.
ES6 : var vs. let and const
This file contains 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
// 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