All declarations are parsed before the code is executed, but you won't be able to access the values stored in let and const until after the declaration line.
There's nothing to "fix" per say, as this is what hoisting is doing for you during the compilation stage of execution. Specifically moving function definitions into memory.
However, I'd say it's best practice to declare functions, before calling as it's easier to reason about the code and reads better in a top down approach. But the JavaScript compiler could care less.
This gives you more freedom on how you structure your files. You're not forced to declare functions on the top, and instead can have the most relevant code on the top which can make the code easier to read.
The declaration and the value are separate. When hoisted, this code would read something like this:
var a;
var a;
a = function() {};
typeof a; // "function"
a = false;
typeof a; // "boolean"
The two instances of var a are declarations of the variable a, and have nothing to do with the values assigned. The function and false are assignments.
20
u/[deleted] Jul 19 '21
function declarations aren't the only thing that is hoisted, variable declarations with
varare also hoisted.e.g. the following does not work: