JavaScript: Determine Type of Object

By Xah Lee. Date: . Last updated: .

How to determine the “type” of an object?

That is, how to find out if a object is function, array, date, regex, etc?

For array, you can do Array.isArray(obj), it returns true for true array.

For function, you can do typeof obj === "function".

Otherwise, use this:

// determine object's subtype
Object.prototype.toString.call(myObj);
ES2015 §Fundamental Objects#sec-object.prototype.tostring

Example:

console.log(
    Object.prototype.toString.call({}) === "[object Object]"
) // true

console.log(
    Object.prototype.toString.call(/./) === "[object RegExp]"
) // true

More example:

console.log ( Object.prototype.toString.call({}) === "[object Object]" ); // true
console.log ( Object.prototype.toString.call([]) === "[object Array]" ); // true
console.log ( Object.prototype.toString.call(/./) === "[object RegExp]" ); // true
console.log ( Object.prototype.toString.call(new Date()) === "[object Date]" ); // true
console.log ( Object.prototype.toString.call(function f() {}) === "[object Function]" ); // true
console.log ( Object.prototype.toString.call(1) === "[object Number]" ); // true
console.log ( Object.prototype.toString.call("") === "[object String]" ); // true
console.log ( Object.prototype.toString.call(true) === "[object Boolean]" ); // true

Here's how it works.

This expression Object.prototype.toString.call(obj) is using the method Object.prototype.toString on the object obj.

We don't want to do obj.toString() because each object may have its own toString property therefore overrides the Object.prototype.toString.

Only the Object.prototype.toString() will print the subtype info. Namely, “[Object Subtype]”.

There's no “logical” reason why Object.prototype.toString() prints that way. It's just the way JavaScript is.

For how the .call() works, See: JavaScript: f.call f.apply f.bind.

What is [[Class]] and What does Object.prototype.toString() do?

The concept of “subtype”, does not technically exist in JavaScript the language. 〔➤see JavaScript: Data Types

In ECMAScript spec 5.1 (2011), object has a internal property denoted “[[Class]]”.

ECMAScript 5.1 §8#sec-8.6.2

Its value is a string. The string indicates the “class” of the object. This “class” is what we think of the subtype of the object.

The function Object.prototype.toString() returns this internal property [[class]], in this string from "[object [[Class]]]"

ECMAScript 5.1 §15#sec-15.2.4.2

The possible return values are:

But in ES2015, the internal property “[[Class]]” is no longer in the spec . However, the behavior of Object.prototype.toString() is kept for backward compatibility.

ES2015 §Fundamental Objects#sec-object.prototype.tostring

ECMAScript 2015 added new kind of objects, such as Set 〔➤see JavaScript: the Set Object (ES2015)〕, Object.prototype.toString() will return expected string.

var u = new Set([3,4])
console.log( Object.prototype.toString.call(u) ) // [object Set]

Warning: Do Not Use 「typeof」, 「instanceof」, 「constructor」

Note: typeof does not work, except for function object. See: JavaScript: Data Types.

Note: do not use instanceof, because it doesn't work. See: JavaScript “instanceof” Operator.

Note: do not use property constructor, because it doesn't work. See: JavaScript: Property Key "constructor"

Object and Inheritance Topic

  1. JavaScript: Object System Overview
  2. JavaScript: What's Object?
  3. JavaScript: Prototype and Inheritance
  4. JavaScript: Creating Object
  5. JavaScript: Object Literal Expression {…}
  6. JavaScript: Creating Object with Object.create()
  7. JavaScript: Find Object's Prototype/Parent
  8. JavaScript: How to Create Object with Parent X?
  9. JavaScript: Prevent Adding/Deleting/Modifying Object Properties
  10. JavaScript: Determine Type of Object
  11. JavaScript: Primitive Types Object Wrapper
  12. JavaScript: Clone Object
  13. JavaScript: Test Equality of Objects
  14. JavaScript: Boolean() Constructor Object
Like what you read? Buy JavaScript in Depth
or, buy a new keyboard, see Keyboard Reviews.