1

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?

Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
jerrypy
  • 139
  • 1
  • 2
  • 14
  • `let` and `var` both are being declared in same scope. So first `let` will declare the variable `a` and then the `var a` will execute. As variable `a` has already been declared using `let`, you will get error for `var`. – Anurag Singh Bisht Oct 11 '17 at 10:55
  • This happens because `let` is hoisted as well and is in *temporal dead zone*. – Estus Flask Oct 11 '17 at 11:06
  • 2
    This doesn't have much to do with hoisting (although clearly "both" variables are declared in the same scope), you just get the exception on the *second declaration* with the same name. – Bergi Oct 11 '17 at 11:57
  • @estus I have read this question, but in [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let#Temporal_dead_zone_and_errors_with_let#Temporal_Dead_Zone_and_errors_with_let) and [Specifation](http://ecma-international.org/ecma-262/6.0/index.html#sec-let-and-const-declarations), clarified that let do not hoist. – jerrypy Oct 11 '17 at 13:28
  • It is 'hoisted' in broader sense of this word, while ES2015 provides tech definition. *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*, I guess it's more correct explanation of what's going on. Until the declaration is evaluated, it's in dead zone and cannot be read or written. – Estus Flask Oct 11 '17 at 13:45
  • @Bergi Thank you very much, I think Javascript just throw the exception at the *second one*. Could you please take a look at [this question](https://stackoverflow.com/questions/46691231/how-does-javascript-declare-function-parameters), haven't received a satisfying answer yet. – jerrypy Oct 11 '17 at 16:04

0 Answers0