As we all know:
If an identifier has already been defined in a scope, then using the identifier in a let declaration inside that scope causes an error to be thrown. -- 《YDFJS》
and var
doesn't have such limit:
var a = 1;
var a = 2; // works fine
Snippet below will fail,
let a = 1;
var a = 2; // SyntaxError: Identifier 'a' has already been declared
Because of the "Variable Hoisting", I think this code will be translated like this:
var a;
let a = 1;
a = 2;
so, the SyntaxError exception should be thrown by the first line: let a = 1
, but in both Node and Chrome, the exception was thrown by the second line: var a = 2
.
I got confused, why is var
doing the redeclaration checking and throw an exception here?