is learning the contents of ES6. I am reading a book published by teacher Ruan Yifeng. I see the contents of the variable declaration about the temporary dead zone. Here is an example:
var tmp = 123;
if (true) {
tmp = "abc"; // ReferenceError:tmp is not defined
let tmp;
}
after reading the analysis of this example, it still didn"t solve my question. I don"t put let"s declaration in front of the code block.
here"s how I look at this example: first, because there are no functions in the code, a global variable tmp is declared and a value of 123 is initialized. Then, after entering the conditional statement, the global variable is reassigned, followed by a declaration of a variable with the same name as the global variable that is valid only in curly braces. In the end, it was wrong. Tmp is not defined, but it is clear that tmp has been declared globally, so the error is reported because it conflicts with the tmp of the local block scope.
then change the code to change the name of the let variable:
var tmp = 123;
if (true) {
tmp = "abc";
let temp;
}
// abc tmp
No error is reported, and abc is output, the temp of the block scope is not affected
the conclusion is that a variable with the same name as a global variable cannot be declared in the code block? It"s obviously not supposed to be like this. I"m a little confused.