Categories
javascript javascript-objects types

Check if a value is an object in JavaScript

1968

How do you check if a value is an object in JavaScript?

10

  • 4

    A variable is a variable. It may refer to an object. Also, you may want to define “object” – as the answers and comments show, there are various conflicting definitions (e.g. whether null is an object).

    – user395760

    Dec 14, 2011 at 20:46


  • 8

    OP, IMO you should accept @Daan’s answer as it is the best solution and should be listed above other answers so it is seen first. (No offense to others who also have good answers.)

    – tiffon

    Nov 17, 2015 at 21:11

  • 3

    IMHO it really depends on what you (the one seeking for an answer to this question) consider an Object, and why you are checking it. This question gives different answers if you are trying to distinguish Arrays (that are Objects) from other Objects or if you are trying to separate scalar values from “vectors”. And whether null (that is an Object, according to typeof) or Functions (that are Objects) should be excluded or not, it really depends on why you are checking it. That’s why there are so many answers and most of them, in context, are correct.

    Feb 8, 2017 at 11:52


  • 1

    It would be great if you could start by saying what exactly you mean by “is an Object”. (Or, say explicitly that part of the answer you’re seeking is to nail down various popular meanings of “is an object” and then differentiate between them.) Lacking that, everyone is talking past each other.

    – Don Hatch

    Dec 26, 2019 at 13:43

  • 1

    @tiffon How about a to the point answer at stackoverflow.com/a/52478680/1096194. I wrote this when I was overwhelmed by seeing the length of detail in many of the highest voted answers. I believe it deserves a lot more visibility.

    Jun 8, 2020 at 10:40


895

UPDATE:

This answer is incomplete and gives misleading results. For example, null is also considered of type object in JavaScript, not to mention several other edge cases. Follow the recommendation below and move on to other “most upvoted (and correct!) answer”:

typeof yourVariable === 'object' && yourVariable !== null

Original answer:

Try using typeof(var) and/or var instanceof something.

EDIT: This answer gives an idea of how to examine variable’s properties, but it is not a bulletproof recipe (after all there’s no recipe at all!) for checking whether it’s an object, far from it. Since people tend to look for something to copy from here without doing any research, I’d highly recommend that they turn to the other, most upvoted (and correct!) answer.

29

  • 143

    This answer is incorrect. typeof returns ‘object’ for null, which is not an object, and instanceof doesn’t work for objects created using Object.create(null).

    – Nikolai

    Apr 8, 2014 at 21:12

  • 3

    typeof nullobject!

    Sep 15, 2014 at 19:14

  • 17

    Arrays will also return as “objects” as in: someArray instanceof Object //true or typeof someArray === 'object' // true. What about: Object.prototype.toString.call(someObject) === "[object Object]", or "[object Array]" if you’re trying to detect an array?

    Jun 19, 2015 at 14:52


  • 8

    @Jonathan, there are better reasons for downvoting my answer, do you by chance have military background? 🙂

    Sep 8, 2015 at 11:54

  • 10

    This should not be the accepted answer. Beyond the stylistic concerns raised by Jonathan, it is simply incorrect and does not bring up the very important subtleties in e.g. @matt-fenwick’s answer.

    Jan 18, 2016 at 20:27

895

UPDATE:

This answer is incomplete and gives misleading results. For example, null is also considered of type object in JavaScript, not to mention several other edge cases. Follow the recommendation below and move on to other “most upvoted (and correct!) answer”:

typeof yourVariable === 'object' && yourVariable !== null

Original answer:

Try using typeof(var) and/or var instanceof something.

EDIT: This answer gives an idea of how to examine variable’s properties, but it is not a bulletproof recipe (after all there’s no recipe at all!) for checking whether it’s an object, far from it. Since people tend to look for something to copy from here without doing any research, I’d highly recommend that they turn to the other, most upvoted (and correct!) answer.

29

  • 143

    This answer is incorrect. typeof returns ‘object’ for null, which is not an object, and instanceof doesn’t work for objects created using Object.create(null).

    – Nikolai

    Apr 8, 2014 at 21:12

  • 3

    typeof nullobject!

    Sep 15, 2014 at 19:14

  • 17

    Arrays will also return as “objects” as in: someArray instanceof Object //true or typeof someArray === 'object' // true. What about: Object.prototype.toString.call(someObject) === "[object Object]", or "[object Array]" if you’re trying to detect an array?

    Jun 19, 2015 at 14:52


  • 8

    @Jonathan, there are better reasons for downvoting my answer, do you by chance have military background? 🙂

    Sep 8, 2015 at 11:54

  • 10

    This should not be the accepted answer. Beyond the stylistic concerns raised by Jonathan, it is simply incorrect and does not bring up the very important subtleties in e.g. @matt-fenwick’s answer.

    Jan 18, 2016 at 20:27

618

Let’s define “object” in Javascript. According to the MDN docs, every value is either an object or a primitive:

primitive, primitive value

A data that is not an object and does not have any methods. JavaScript has 7 primitive data types: string, number, bigint, boolean, undefined, symbol, and null.

What’s a primitive?

  • 3
  • 'abc'
  • true
  • null
  • undefined

What’s an object (i.e. not a primitive)?

  • Object.prototype
  • everything descended from Object.prototype
    • Function.prototype
      • Object
      • Function
      • function C(){} — user-defined functions
    • C.prototype — the prototype property of a user-defined function: this is not Cs prototype
      • new C() — “new”-ing a user-defined function
    • Math
    • Array.prototype
      • arrays
    • {"a": 1, "b": 2} — objects created using literal notation
    • new Number(3) — wrappers around primitives
    • many other things
  • Object.create(null)
  • everything descended from an Object.create(null)

How to check whether a value is an object

instanceof by itself won’t work, because it misses two cases:

// oops:  isObject(Object.prototype) -> false
// oops:  isObject(Object.create(null)) -> false
function isObject(val) {
    return val instanceof Object; 
}

typeof x === 'object' won’t work, because of false positives (null) and false negatives (functions):

// oops: isObject(Object) -> false
function isObject(val) {
    return (typeof val === 'object');
}

Object.prototype.toString.call won’t work, because of false positives for all of the primitives:

> Object.prototype.toString.call(3)
"[object Number]"

> Object.prototype.toString.call(new Number(3))
"[object Number]"

So I use:

function isObject(val) {
    if (val === null) { return false;}
    return ( (typeof val === 'function') || (typeof val === 'object') );
}

@Daan’s answer also seems to work:

function isObject(obj) {
  return obj === Object(obj);
}

because, according to the MDN docs:

The Object constructor creates an object wrapper for the given value. If the value is null or undefined, it will create and return an empty object, otherwise, it will return an object of a type that corresponds to the given value. If the value is an object already, it will return the value.


A third way that seems to work (not sure if it’s 100%) is to use Object.getPrototypeOf which throws an exception if its argument isn’t an object:

// these 5 examples throw exceptions
Object.getPrototypeOf(null)
Object.getPrototypeOf(undefined)
Object.getPrototypeOf(3)
Object.getPrototypeOf('abc')
Object.getPrototypeOf(true)

// these 5 examples don't throw exceptions
Object.getPrototypeOf(Object)
Object.getPrototypeOf(Object.prototype)
Object.getPrototypeOf(Object.create(null))
Object.getPrototypeOf([])
Object.getPrototypeOf({})

11

  • 43

    obj === Object(obj) returns true for arrays.

    Feb 15, 2016 at 17:10

  • 6

    var x = []; console.log(x === Object(x)); // return true

    Apr 8, 2016 at 7:51


  • 18

    @Illuminator arrays are objects in Javascript, as I mentioned in my answer.

    Apr 8, 2016 at 14:20

  • 1

    getPrototypeOf does not work e.g. with revoked proxies, which are objects but throw.

    – Oriol

    Aug 28, 2016 at 1:03

  • 6

    Why not ({}).toString.apply(obj) === '[object Object]' this distinguishes between arrays and objects that are not arrays

    Nov 6, 2018 at 22:09