Description

let allows you to declare variables, limiting its scope to the block, statement, or expression on which it is used. This is unlike the var keyword, which defines a variable globally, or locally to an entire function regardless of block scope.

Block scoping

let declared variables are hoisted to the beginning of the enclosing block.

Redeclaration of the same variable in the same block scope raises a TypeError.

if (x) {
let foo;
let foo; // TypeError thrown.
}

However, function bodies do not have this limitation!

function do_something() {
let foo;
let foo; // This works fine.
}

Warning: ECMAScript 6 drafts (as of April, 2012) make this behavior illegal. That means that future versions of JavaScript will likely be consistent and raise a TypeError if you do this, so you should avoid this practice!

You may encounter errors in switch statements because there is only one underlying block.