How can I check if a value is an object in JavaScript?

What’s the most reliable way to determine if a value is a plain object?

What’s a clean and reliable way to check if a value is a plain object in JavaScript without mistakenly identifying arrays, null, or other types as objects?

A simple way to check for objects is by using the typeof operator, but it will also return “object” for null and arrays. So, we need an additional check for null.

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

console.log(isObject({})); // true
console.log(isObject([])); // true (array is an object)
console.log(isObject(null)); // false
console.log(isObject('string')); // false

Why this works?

It checks if the value is not null and confirms that it is an object. However, this approach will still return true for arrays, so it’s not perfect for distinguishing plain objects from arrays.

If you want to make sure that you’re checking for plain objects and not arrays, you can combine the typeof check with Array.isArray().

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

console.log(isPlainObject({})); // true
console.log(isPlainObject([])); // false (array is excluded)
console.log(isPlainObject(null)); // false

Why this works?

This method ensures that the value is an object and also specifically checks that it’s not an array. It’s a solid approach for filtering out non-plain objects like arrays and null.

For a more foolproof approach, you can use Object.prototype.toString.call(), which provides a reliable way to check the internal [[Class]] property of a value. This will exclude arrays and null entirely.

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

console.log(isPlainObject({})); // true
console.log(isPlainObject([])); // false
console.log(isPlainObject(null)); // false

Why this works:

Object.prototype.toString.call() is a more robust method for distinguishing plain objects from other types.

It returns a string like [object Object] for plain objects and [object Array] for arrays, giving a clean and reliable way to check for a plain object.