0

I can identify the following types in JavaScript:

number, string, boolean, object, null and undefined

But, is function a discrete type, or is it an instance of object?

6
  • 4
    Functions are instances of the Function constructor. They're objects with special runtime-supported characteristics (like, being functions). Commented Feb 17, 2015 at 22:46
  • Also typeof considers null to be of type Object, and undefined really isn't a type at all. Commented Feb 17, 2015 at 22:47
  • So the return value of typeof correspond to a superset of the types in the language? Commented Feb 17, 2015 at 22:53
  • Personally I think typeof is somewhat broken. A better tool to use is the .toString() function on the Object prototype: ({}).toString.call(yourValue) gives you a slightly cumbersome but less weird result than typeof. Commented Feb 17, 2015 at 22:56
  • 1
    Well everything that's not a scalar type - numbers, booleans, and strings - that's not null or undefined is an object (actually the values involved are references to the objects - objects are never directly "values"). Commented Feb 17, 2015 at 23:03

1 Answer 1

1

Functions in javascript are inherited from Object. You can try the following :

var my_func = function(){};
console.log(my_func instanceof Object); // prints true
console.log(my_func instanceof Function); // also prints true

The same is true for Array.

Sign up to request clarification or add additional context in comments.

Comments

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.