JS: “function” (keyword) 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 keyboard
var, then, only hoist the name, not the definition.
Compare:
console.log(ff()); // prints // 3 function ff() { return 3; }
console.log(gg()); var gg = function gg() { return 3; }; // error: Uncaught (in promise) ReferenceError: Cannot access 'gg' before initialization