Skip to content

Instantly share code, notes, and snippets.

@nuragic
Created October 31, 2017 23:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nuragic/c9221ab85bb17fc0c4c1d7e8cb11cb0e to your computer and use it in GitHub Desktop.
Save nuragic/c9221ab85bb17fc0c4c1d7e8cb11cb0e to your computer and use it in GitHub Desktop.
Why let is the new var?
// Exists since ES6
let isTheNewVar = '☝️😙';
function why() {
let result = 'Well, actually…';
if (!window.isTheNewVar) {
let isTheNewVar = 'BECAUSE IT CANNOT CREATE "GLOBAL" PROPERTIES! 🎉';
let result = 'ALSO BECAUSE IT IS BLOCK-SCOPED! 😎';
setTimeout(() => { console.log(`${isTheNewVar} ${result}`) }, 0);
}
console.log(`Why?!`);
console.log(result);
}
/* guess */ why() /* ? */
setTimeout(() => { console.log(`OH, AND IT CANNOT BE RE-DECLARED (you'll see in 3,2,1… 💥)`) }, 0);
setTimeout(() => {
let uh = '👀';
let uh = '👅';
}, 3000);
setTimeout(() => {
console.log(`Bonus: NO HOISTING ANYMORE! 😱 Wait… now there's a thing called "Temporal Dead Zone" 😬`);
try {
console.log(`TDZ: ${TDZ}`);
let TDZ = '💀';
} catch(err) {
console.error(`Ups! ${err}`);
// Mmmmh-ake sense! BUT REMEMBER... IT'S TEMPORAL, NOT SPATIAL:
function WAT() {
console.log(`Messing with TDZ: ${TDZ}`);
}
let TDZ = '💀';
WAT();
}
}, 6000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment