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?

Check If Object is Array

Array.isArray(obj) → returns true for true array.

〔►see JavaScript: Array-Like Object

Check If Object is Function

typeof obj === "function"


The most reliable and generic way to determine the type of object is this: ( obj )

The obj can be any value, such as 3.

The result is one of the following:

(there are more, such as WeakMap, WeakSet, Proxy, Promise, etc)

〔►see JavaScript: Object.prototype.toString

WARNING: If a obj is a object type, and has a symbol key property Symbol.toStringTag, either own property or inherited, and if its value is a string, let's say tag, then the result of ( obj ) is the string:

"[object tag]"

〔►see JavaScript: Symbol Tutorial

For detail, see JavaScript: Object.prototype.toString

How it Works is using the method Object.prototype.toString on the object obj.

〔►see JavaScript: Function Call, Apply, Bind

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

〔►see JavaScript: Object.prototype.toString

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

Note: typeof does not work, because it doesn't distinguish different types of objects. 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: Create Object
  5. JavaScript: Object Literal Expression
  6. JavaScript: Find Object's Prototype
  7. JavaScript: How to Create Object with Parent X?
  8. JavaScript: Prevent Adding Property
  9. JavaScript: Determine Type of Object
  10. JavaScript: Primitive Value Object Wrapper
  11. JavaScript: Clone, Deep Copy Object/Array
  12. JavaScript: Test Equality of Objects

  1. JavaScript: Object Object
  2. JavaScript: Object.prototype
Like what you read? Buy JavaScript in Depth
or, buy a new keyboard, see Keyboard Reviews.