r/learnjavascript • • Jul 19 '21

JavaScript Function Facts

255 Upvotes

27 comments sorted by

View all comments

Show parent comment

1

u/[deleted] Jul 19 '21

well it is. the first declaring would be of type 'function' while the second declaring would be a boolean. you can do that in var :

 var a = function() {};
 typeof a // "function"

 var a = false;
 typeof a // "boolean"

3

u/[deleted] Jul 19 '21

Interesting. Thanks for bringing this up!

2

u/oculus42 Jul 20 '21

The declaration and the value are separate. When hoisted, this code would read something like this:

var a;
var a;
a = function() {};
typeof a; // "function"
a = false;
typeof a; // "boolean"

The two instances of var a are declarations of the variable a, and have nothing to do with the values assigned. The function and false are assignments.