Trying to get an understanding of hoisting in JS and can't seem to understand how hoisting and global/local variables are working in this simple function.
let x = 7;
function timesTwo(num){
console.log("first ", x);
x = num * 2;
console.log("second " , x);
return x;
}
timesTwo(2); // first 7
// second 4
timesTwo(3); // first 4
// second 6
// 6
I know this is not a great way to write out the function. But why does the function not return a value the first time it is run, even though console.log seems to know what the x variable is, and then why is there a return the second time the function runs?