Comparison
HoistingvsTemporal dead zone
Hoisting
you called the function on line three and it worked, even though it is declared on line forty.
Declarations being processed before any code in their scope runs, so a function declaration is callable above its own definition. `var` is hoisted too but initialised to undefined, which is the source of the classic bug where a variable exists and is empty rather than throwing. `let` and `const` are hoisted without initialisation, which is what produces the temporal dead zone instead.
Full entry →Temporal dead zone
you got 'cannot access before initialization' on a `const` that is clearly declared right there in the file.
The window between entering a scope and the point where a `let` or `const` is initialised, during which reading it throws instead of returning undefined. It exists to turn a silent `var` bug into a loud error. It surprises people in circular module imports and in class field ordering, where the access happens earlier than reading the file top to bottom suggests.
Full entry →