JS: “function” name hoisting
Function Declaration Hoist Name and Value
for functions defined by the keyword function, there are these complex issues:
- If it is written as a function declaration, it implicitly move the function name and definition to the top. (known as name-hoisting.)
- If it is written as a function expression assigned to variable by the keyword var , then only hoist the name, not the definition. if you use const or let , there is no any hoisting. Function call code must happen after it.
Compare:
console.log(ff()); // prints // 3 function ff() { return 3; }
console.log(gg()); var gg = function gg() { return 3; }; // error: Uncaught TypeError: gg is not a function