Var console let console directly reported an error?

Why does let with no variable promotion affect statements before declaration? < / H2 >

related codes

var a = 0
console.log (a)
let a = 9
console.log (a)
VM102:3 Uncaught SyntaxError: Identifier"a "has already been declared
the error is reported directly on the third line, but there is no output on the second line

.

after adding other console code at the top, it still reports an error directly.
does it seem that let"s declaration has still been promoted to the top?
Test environment (Safari 12) (chrome 69)

there is no restriction on repeated declaration in PS: var

var a = 0
console.log (a) / / 0
var a = 9
console.log (a) / / 9

Aug.24,2021

this is because in the console, when you declare variables using let const , you follow your own defined rules, have block-level scope (duplicate variable names are not allowed to be declared), and the defined variables are not destroyed.


simply means that if you declare a variable with const and let, the code in the block is checked before the code runs to see if any variable names are duplicated with let and const. The official standard saying is that let does not allow repeating declaration of the same variable within the same scope.

Menu