3

Here is a simple example of a function and its datatype:

Code Snippet:

function hello(){
    console.log("hello");
}
console.log(typeof(hello)); //function

Query:

In JavaScript these are the datatypes:

1. Object
2. Primitives - String, number, boolean, null, undefined, symbol(ECMA6) 

When there is no datatype named 'function' then how in the world am I getting the datatype as function (though logically its correct). Also, textually it is written that datatype of function is object. So, I must have got result as object.

Can someone clarify this confusion?

3
  • The typeof operator returns a string indicating the type of the unevaluated operand., Not really a data type Commented Sep 13, 2016 at 7:19
  • Because "function" is what the spec says to return? (Which makes sense, whereas typeof null returns "object".) Commented Sep 13, 2016 at 7:19
  • Yes both of these cases are confusing. Also, is there any other way to test the datatype of a variable other than typeof -- as it returns spec or indicates type of unevaluated operand? (As per the above 2 comments) Commented Sep 13, 2016 at 7:25

2 Answers 2

4

Yes, you're right, there's no function type, it's just a hack in the typeof operator that returns "function" for objects that are callable.

Reference: http://www.ecma-international.org/ecma-262/6.0/#sec-typeof-operator

Generally speaking, typeof does return the actual type name, with two exceptions:

  • typeof null returns object, but the type of null is Null
  • typeof <func> returns function, but the type of <func> is Object
Sign up to request clarification or add additional context in comments.

4 Comments

This means that the correct answer should have been object only, but in the current scenario due to some specifications -- "It is giving function", but in truth it should have been "Object"?
Also, add typeof(Nan) === "number" in the exception list!
But, I thought its real semantic meaning is "not a number"! Then how can its datatype be a "number"! Confused ...
@Peterson: yep, NaN is a "Number" which is not a number. Confusing indeed, but that's how it works.
2

From the "You don't know JS book" by Kyle Simpson:

So what’s the seventh string value that typeof can return?

typeof function a(){ /* .. */ } === "function"; // true

It’s easy to think that function would be a top-level built-in type in JS, especially given this behavior of the typeof operator. However, if you read the spec, you’ll see it’s actually somewhat of a “subtype” of object . Specifically, a function is referred to as a “callable object”— an object that has an internal [[Call]] property that allows it to be invoked.

It was made to differentiate "callable" objects from "non-callable" ones.

2 Comments

Seeing this, I believe function should have been a datatype itself or at-least instead of returning function --- typeof return should have been "callable object" -- That would have made better sense!
Functions have some object behavior - it has prototype, properties (like "length" - the number of arguments, "name", etc) and methods (including object-specific "toString", "valueOf"), and that's might be the reason why they are a subtype of object instead of a separate type. The "function" word is likely used to avoid confusion for people coming from another languages.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.