Es6's question about temporary Dead Zone

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.

May.09,2022

when the JavaScript engine scans the code to find variable declarations, it either promotes them to the top of the scope (encountered var declarations) or places them in TDZ (encountered let and const declarations). Accessing variables in TDZ triggers a run-time error. Variables are not removed from TDZ until the variable declaration statement has been executed before they can be accessed normally.

var tmp = 123;

if (true) {
  tmp = 'abc'; // ReferenceError:tmp is not defined
  let tmp;
}

when accessing tmp in if, you are accessing tmp in TDZ, not global tmp.


you obviously don't use any TDZ concept when you think about problems! You are watching TDZ, but still think according to your own original understanding, so how can you learn? Just publish your own book, which is called "javascript I understand"


as long as the let command exists in the block-level scope, the variables it declares are "bound" to this area and are no longer affected by external influences.
within the code block, the variable is not available until it is declared with let. Grammatically, it is called a "temporary dead zone". (temporal dead zone)

ES6 stipulates temporary dead zones and no variable promotion in let and const statements, mainly to reduce run-time errors and prevent the use of this variable before variable declaration, resulting in unexpected behavior.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1befe42-31cce.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1befe42-31cce.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?