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

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.