HOWTO · JavaScript

How to Check if a Value Is Object in JavaScript

In this tutorial we will check if a value is an object in JavaScript

On this page

To test whether a JavaScript value is an object, use typeof value === "object" && value !== null. This predicate includes arrays and other objects, but excludes null and functions. If your code should also accept functions, add typeof value === "function" to the condition. Choose the rule that matches what your program needs: JavaScript treats functions as callable objects, while typeof reports them separately.

Use instanceof to Check Whether a Value Is an Object in JavaScript

instanceof is an operator that checks whether a constructor’s prototype appears in a value’s prototype chain. For a plain object created in the same JavaScript realm, value instanceof Object is often true. This makes it useful when you want to check for instances associated with a particular constructor, such as value instanceof Date.

It is not a universal object predicate. An object created with Object.create(null) has no Object.prototype in its chain, so it fails instanceof Object. Objects created in another realm, such as a different browser window or iframe, also have different constructors and prototypes; an object from that realm can fail the local realm’s instanceof Object check. Use a predicate based on typeof when you need a general object check across these cases.

Use typeof to Check Whether a Value Is an Object in JavaScript

typeof is an operator, not a function, and it does not need parentheses around its value. For ordinary objects, including arrays, typeof value returns "object". There are two results to keep in mind: typeof null is also "object", while typeof a function is "function". Therefore, comparing only with "object" includes null and excludes functions. Add an explicit null check when you want objects but not null values.

Define a Reusable Object Check

This predicate returns true for non-null objects and false for functions, primitives, and null. The example shows its result for an object, a null value, a number, and a function.

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

const person = { name: "Ada" };
console.log(isObject(person));
console.log(isObject(null));
console.log(isObject(42));
console.log(isObject(() => {}));

The calls print one Boolean result each in the same order as the inputs. This definition intentionally answers “is this a non-null object?” If a function should count for your use case, use value !== null && (typeof value === "object" || typeof value === "function"). Avoid checking value.constructor.toString() for this purpose: a value may not have the expected constructor, and constructor names or source text are not a reliable general type test. The Object(value) === value pattern also has conversion behavior that can obscure the predicate’s intended contract.

Use Object.getPrototypeOf() to Inspect an Object’s Prototype

Object.getPrototypeOf(value) answers a different question: it returns the prototype of an object. For example, Object.getPrototypeOf(Object.create(null)) returns null, while Object.getPrototypeOf({}) returns Object.prototype. This can help inspect inheritance or compare a value’s prototype with a known prototype, but it is not a replacement for an object predicate. Passing null or undefined to Object.getPrototypeOf() causes a TypeError; other primitive values are coerced to objects before their prototype is returned. Use this method only when you need to inspect a prototype, not as a general object predicate.