Search code examples
javascriptfunctionobjecttypeof

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

Solution

  • 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.