0
var a=2;
function a(){};
console.log(typeof(a));

I am wondering why the result of the above code is number. I know the var is hoisted but isn't it already at the first line of the code? so why the type of a is still a number instead of a function?

Alexzhang
  • 99
  • 6

1 Answers1

2

In this case function a(){} is actually hoisted, which you can see if you log a at various points:

console.log(a);
var a = 2;
console.log(a);
function a() {
    console.log('hi');
}


console.log(a);
dave
  • 62,300
  • 5
  • 72
  • 93