Skip to content

Instantly share code, notes, and snippets.

@wklug
Created October 14, 2016 21:09
Show Gist options
  • Save wklug/232db82c85d4f2bc16ab33dd8018fcf4 to your computer and use it in GitHub Desktop.
Save wklug/232db82c85d4f2bc16ab33dd8018fcf4 to your computer and use it in GitHub Desktop.
ES6 - var, let, const. The temporal dead zone.
/**
* ES6 - var, let, const. The temporal dead zone.
*
* According to ECMAScript 6 specification. https://tc39.github.io/ecma262/#sec-let-and-const-declarations
* let and const declarations define variables that are scoped to the running execution context's
* LexicalEnvironment. The variables are created when their containing Lexical Environment is instantiated but may
* not be accessed in any way until the variable's LexicalBinding is evaluated. A variable defined by a
* LexicalBinding with an Initializer is assigned the value of its Initializer's AssignmentExpression when the
* LexicalBinding is evaluated, not when the variable is created. If a LexicalBinding in a let declaration does
* not have an Initializer the variable is assigned the value undefined when the LexicalBinding is evaluated.
*
* var, let, and const are hoisted. But let and const cannot be accessed before the actual declaration is
* evaluated at runtime.
**/
console.log(favoriteDrink); // undefined
var favoriteDrink = 'Beer 🍺';
console.log(favoriteFood); // ReferenceError
const favoriteFood = 'Taco 🌮';
console.log(favoriteDessert); // ReferenceError
let favoriteDessert = 'Cookie 🍪';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment