I'm new to JavaScript and am trying to understand the concepts of hoisting and scope.
Case 1
var name;
function logName(num) {
  console.log(name, num);
}
logName(1);
name = "Auro";
logName(2);and I have the following output in the console
Auro 1
Auro 2
If I understand hoisting correctly, JS engine hoists the declaration of a variable first and then automatically assigns a value undefined to it. Only when it encounters the assignment (=) operator, does it actually assign the intended value to it.
If my understanding above is correct, then the first time the logName function was called, the variable name should have printed undefined, but it is printing the value.
Case 2
var name;
function logName(num) {
  console.log(name, num);
}
logName(1);
name = "Auro";
logName(2);
name = "Baner";Output in the console:
Baner 1
Auro 2
This confuses me even more. Here the first call to the function logName picked the later assignment to the variable and printed Baner, however, the second call picked the previous assignment, that is Auro.
What is going on here? What am I missing?
 
     
     
    