Created
October 31, 2017 23:25
-
-
Save nuragic/c9221ab85bb17fc0c4c1d7e8cb11cb0e to your computer and use it in GitHub Desktop.
Why let is the new var?
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
// 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