Skip to content

Instantly share code, notes, and snippets.

@aaronfrost
Created September 10, 2014 07:55
Show Gist options
  • Save aaronfrost/8d1d1e7e97a6f3780ba8 to your computer and use it in GitHub Desktop.
Save aaronfrost/8d1d1e7e97a6f3780ba8 to your computer and use it in GitHub Desktop.
Is this how LET should work?
let a = 0;
{
console.log(a); //Node throws error cause "a" isn`t defined yet, but it was defined on line one.
let a = 1;
console.log(a);
}
console.log(a);
@rwaldron
Copy link

Read it like this:

// In outer scope, an `a` binding is created.
let a = 0;
{
  // Block is entered 
  // All declarations are collected, and an inner `a` binding is created, which will shadow the outer `a`. 
  // The inner `a` TDZ begins now.
  // The [[Get]] on `a` throws because it's before the declaration
  console.log(a);
  // The `a` binding is "officially" declared and given an assignment, 
  // use of the binding in this block is totally ok now
  let a = 1;
  console.log(a);
}
console.log(a);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment