This is because the JavaScript interpreter interprets the code in two cycles.
- Code completion / compilation:
- Code Execution:
In the 1st cycle, all declarations of variables and functions are transferred to the upper part of the region of the function in which they are executed. This helps in creating variableObjects objects for the execution context the function's execution even before it is executed.
At the second stage, assignment of values, code operators, and function calls occur line by line as expected.
You have a slightly more detailed reading here.
This will give you a better idea of the behavior around let , const and class declarations, as well as the priority that follows between a variable and functions.
Jaspreet Singh Mar 02 '19 at 13:50 2019-03-02 13:50
source share