Skip to content

Instantly share code, notes, and snippets.

@rwaldron
Last active April 4, 2018 21:20
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save rwaldron/f0807a758aa03bcdd58a to your computer and use it in GitHub Desktop.
Temporal Dead Zone
{
// The block has begun, we're in a new block scope. The TDZ for the "a" binding has begun
var f = function() {
// 2. Because f() is evaluated before `a` is actually declared,
// an exception will be thrown indicating to the author that
// `a` is not yet defined.
console.log(a);
};
f(); // 1. f is called and results evaluated before the next line...
let a = 1;
// The `a` binding is now safe, but we never got here because f() killed us :(
// that's ok, because we can fix this bug and celebrate the win!
}
{
// The block has begun, we're in a new block scope. The TDZ for the "a" binding has begun
// Declare `a` before use.
let a = 1;
var f = function() {
// 2. Because f() is evaluated after `a` is declared,
// the value of `a` will be logged.
console.log(a);
};
f(); // 1. f is called and results evaluated
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment