I'm trying to wrap my head around a few things when it comes to hoisting and variable mutation.
- Variable assignment takes precedence over function declaration
- Function declarations take precedence over variable declarations
Function declarations are hoisted over variable declarations but not over variable assignments according to https://scotch.io/tutorials/understanding-hoisting-in-javascript. This is extremely confusing because I thought only variable declarations and function declarations get hoisted.
var double = 22;
function double(num) {
return (num*2);
}
console.log(typeof double); // Output: number
so with hoisting it should look like
function double(num) {
return (num*2);
}
var double;
double = 22;
console.log(typeof double); // Output: number
If variable assignment takes precedence over function declaration why isn't double = 22 above the function declaration? Another example
var double;
function double(num) {
return (num*2);
}
console.log(typeof double); // Output: function
With hoisting pulling the function above the variable declaration the console should log undefined shouldn't it? With variable mutation the last assignment is what the program should execute.