-1

I have code like below:

var showName;
function showName() {
};
typeof showName; // function

And we know that JS engine automatically assigns undefined value to variable decalred with var, so in my mind I assume below code will output the same with above(but actually not)

var showName = undefined;
function showName() {
};
typeof showName; // undefined

Any explanation will be thankful

  • Assignment with `=` is not hoisted, but function declarations are – CertainPerformance May 17 '22 at 02:52
  • Also, `var showName` will declare a variable with a default value of `undefined` _if the variable doesn't already have a value_. It is not synonymous with `var showName = undefined`, which assigns `undefined` unconditionally. – Amadan May 17 '22 at 02:54
  • The second code is equivalent to `var showName; function showName() {}; showName = undefined; typeof showName` (`var` and `function` are hoisted, assignment is not). – Amadan May 17 '22 at 02:56

0 Answers0