1

"let" in console As shown above, a variable got locked in console after declared with error. Neither can I assign nor re-declare it.

However, everything works just fine when using "var". In this answer, @ThinkingStiff explained it:

Redeclaration:
Assuming strict mode, var will let you re-declare the same variable in the same scope. On the other hand, let will not.

So I wonder what feature of "let" produces this lock.

My guess: "let" first creates a temporal dead zone then assigns it with the statement all in one operation(one continuous series). Error occurred in the assignment statement didn't break the dead zone.

ps. I'm using Chrome 71.

haopeng
  • 243
  • 1
  • 8
  • The `.getElementById()` function requires a string argument; what could it possibly mean to call it without one? – Pointy Mar 21 '19 at 03:16
  • Also the console is a weird environment that's useful for debugging but not really the same as an actual block of JavaScript imported into a web page. – Pointy Mar 21 '19 at 03:17
  • @Pointy I imagine it's just there as an example of when the right-hand side of the `=` throws an error, could substitute it with anything else that throws and the same behavior will be seen – CertainPerformance Mar 21 '19 at 03:17
  • @CertainPerformance ok that's certainly possible, but again the console is a weird place to test JavaScript semantics because it's not designed to do that. – Pointy Mar 21 '19 at 03:18
  • Also the Firefox console does not behave that way. After a failed `let` initialization, the symbol is still declared. – Pointy Mar 21 '19 at 03:19

0 Answers0