The worm The worm - 1 month ago 6
Javascript Question

Are JavaScript functions objects?

If JavaScript functions are first class objects and therefore of type object why does the below happen?

function hello(){}
typeof hello -> function


should it not give

typeof hello -> object

Answer

Yes, JavaScript functions are objects. The only base types in JavaScript are the primitive value types: number, symbol, boolean, null, undefined and string and objects.

Everything that is not a primitive value type is an object. typeof is broken for other reasons, for example typeof null is "object" but null is in fact not an object.

typeof hello returns function because it's probably the only way to really be sure something can be called as a function.

Comments