The reason for this is that the javascript interpreter processes code in two stages.
- Code completion/compilation:
- Code execution:
During the first stage, all variable and function declarations are hoisted to the top of the function scope it is running in. This allows for the creation of variable objects for the execution context of the function before it is actually executed.
In the second phase, value assignments, code statements, and function calls occur line by line in the expected order.
To delve into more detail, you can read here.
This will provide a deeper understanding of the behavior related to let, const, and class declarations, as well as the precedence between variables and functions.