Check if a value is an object in JavaScript

OpenJavaScript 0
Reading Time: 3 minutes 🕑

Last updated: April 22, 2022.

It is surprisingly complicated to check if a value is a custom object in JavaScript, like the one below:

const myObject = {
    name: "Spiderman",
    city: "New York",
    occupation: "🕸️", 
}

In this post, we show you three ways to achieve this, including a three-way performance test to find out which method is fastest.

Table of contents

Conditional check for a pure object

First, we test if a typeof check on the object returns true. To avoid any unexpected errors, we check for strict equality:

/* Check for type object */

function isObject(value) {
   return typeof myObject === 'object';
}

const array = [1,2,3];
const myObject = {prop1: "value"};
const nullValue = null;

isObject(myObject); // true
isObject(array); // true 😱
isObject(nullValue); // true 😱

This works, but arrays are also a type of object. So we need to add an extra condition to check if the object is not an array:

/* Check for type object and non-array */

function isObject(value) {
   return typeof myObject === 'object' &&
   !Array.isArray(myObject);
}

const array = [1,2,3];
const myObject = {prop1: "value"};
const nullValue = null;

isObject(myObject); // true
isObject(array); // false
isObject(nullValue); // true 😱

This would be okay, but we still need to rule out that myObject is of type null, which is classified as type object in JavaScript.

So, we add a check that myObject isn’t null:

/* Check for type object, non-array and not null ✔️ */

function isObject(value) {
   return typeof myObject === 'object' &&
   !Array.isArray(myObject) &&
   myObject !== null;
}

const array = [1,2,3];
const myObject = {prop1: "value"};
const nullValue = null;

isObject(myObject); // true 
isObject(array); // false
isObject(nullValue); // false

And that’s it: this function can now be used to reliably distinguish between pure objects and other object types in JavaScript.

Update (22/04/2022): Alternative methods

Since circulating this post on our social media channels, we received comments suggesting two alternative methods!

Alternative #1 (suggested by Иван Вохмин)

One alternative is to convert the object to a string with JSON.stringify() and then check if the first character of the resulting string is { (the first character of a string literal):

function isObject(value) {
  return JSON.stringify(value).startsWith("{");
}

const array = [1,2,3];
const myObject = {prop1: "value"};
const nullValue = null;

isObject(myObject); // true
isObject(array); // false
isObject(nullValue); // false

Alternative #2 (Juriy Zaytsev)

We also received a reference to an article by Juriy Zaytsev (@kangax) suggesting the following solution.

In case you are unfamiliar, the .call() method allows the method on one object to be used on another. In this case, .toString from the prototype of the in-built Object class is used with myObject.

function isObject(value) {
  return Object.prototype.toString.call(value) === "[object Object]";
}

const array = [1,2,3];
const myObject = {prop1: "value"};
const nullValue = null;

console.log(isObject(myObject)); // true
console.log(isObject(array)); // false
console.log(isObject(nullValue)); // false

Speed test ⚡

When you have several options available for getting a job done, one criterion you may use to decide between them is performance.

We ran a speed test at JSBEN.CH to determine which method is fastest.

It turns out that alternative 1, which uses JSON.stringify(), may be short syntax, but it is suboptimal from a performance perspective.

It was tight between alternative 2 and the three-part comparison method originally suggested by this article, but the more visually convoluted three-part comparison proved fastest.

Performance test of object checking alternatives

It should be noted that the speed test results reported here may vary slightly by browser. The difference between the three-part comparison and alternative 2 may therefore be too close to call.

Summary

Distinguishing between JavaScript data objects and other object types (e.g. arrays) requires some work.

However, there are ways this can be done. Use any of the three functions featured in this post to achieve this in your project.

Related links