Last modified: Jul 22, 2026
Check if Variable is Object in JS
In JavaScript, checking if a variable is an object is a common task. Objects are fundamental to the language. They store data in key-value pairs. But not everything that seems like an object actually is one.
This guide will show you reliable ways to check for objects. We will cover typeof, instanceof, and the most accurate method using Object.prototype.toString. Each method has its strengths and weaknesses.
Why You Need to Check for Objects
You might receive data from an API. You might loop through properties. Or you might want to call a method safely. Checking if a variable is an object prevents errors.
For example, trying to access user.name on null or a string will throw an error. A proper check avoids this. It makes your code robust and predictable.
Understanding variable types is crucial. If you are new to JavaScript, read our JavaScript Variable Types Guide first. It explains the basics of primitive vs reference types.
Method 1: Using typeof
The typeof operator is the simplest way. It returns a string indicating the type. For objects, it returns "object". But it has a major flaw.
typeof null also returns "object". This is a well-known JavaScript bug. So typeof alone is not enough. You must also check for null.
Here is an example:
// Simple check using typeof
let user = { name: "Alice" };
let empty = null;
let num = 42;
if (typeof user === "object" && user !== null) {
console.log("user is an object");
} else {
console.log("user is not an object");
}
if (typeof empty === "object" && empty !== null) {
console.log("empty is an object");
} else {
console.log("empty is not an object");
}
if (typeof num === "object" && num !== null) {
console.log("num is an object");
} else {
console.log("num is not an object");
}
user is an object
empty is not an object
num is not an object
This works for plain objects. But it fails for arrays. typeof [] also returns "object". So this method is good for a quick check. But it is not perfect.
Method 2: Using instanceof
The instanceof operator checks the prototype chain. It returns true if the object is an instance of a specific constructor. For generic objects, you use instanceof Object.
This method works well for objects created with {} or new Object(). But it also has limitations. It does not work across different realms (e.g., iframes). And it returns true for arrays and functions.
// Using instanceof
let person = { name: "Bob" };
let list = [1, 2, 3];
let greet = function() { return "hi"; };
console.log(person instanceof Object); // true
console.log(list instanceof Object); // true (arrays are objects)
console.log(greet instanceof Object); // true (functions are objects)
console.log(null instanceof Object); // false
true
true
true
false
As you see, instanceof considers arrays and functions as objects. This is technically correct. But if you want to exclude them, you need extra checks. For example, use Array.isArray() for arrays.
Method 3: The Most Accurate Way (toString)
The most reliable method uses Object.prototype.toString.call(). This returns a string like "[object Object]" for plain objects. It works across realms. It distinguishes between arrays, functions, dates, and plain objects.
This is the gold standard. It is used in many libraries. It is safe and predictable.
// Most accurate check
function isPlainObject(value) {
return Object.prototype.toString.call(value) === "[object Object]";
}
let obj = { x: 1 };
let arr = [1, 2];
let date = new Date();
let fn = () => {};
let nothing = null;
console.log(isPlainObject(obj)); // true
console.log(isPlainObject(arr)); // false
console.log(isPlainObject(date)); // false
console.log(isPlainObject(fn)); // false
console.log(isPlainObject(nothing)); // false
true
false
false
false
false
This method only returns true for plain objects. It excludes arrays, dates, and functions. This is perfect for most use cases. If you need to include other object types, adjust the string comparison.
Edge Cases to Consider
JavaScript has many special values. null and undefined are not objects. But typeof null is "object". Always check for null explicitly.
Arrays are objects. But they have special behavior. If you need to check for an array, use Array.isArray(). Functions are also objects. They are callable objects.
Another edge case is objects created with custom constructors. instanceof works for them. But toString still returns "[object Object]" unless you override Symbol.toStringTag.
Practical Example: Safe Property Access
Here is a real-world example. You fetch data from an API. You want to access user.profile.name. A safe check prevents crashes.
// Safe property access
function getProfileName(data) {
if (typeof data === "object" && data !== null) {
if (typeof data.profile === "object" && data.profile !== null) {
return data.profile.name;
}
}
return "Unknown";
}
let validData = { profile: { name: "Charlie" } };
let invalidData = null;
let partialData = { profile: "not an object" };
console.log(getProfileName(validData)); // Charlie
console.log(getProfileName(invalidData)); // Unknown
console.log(getProfileName(partialData)); // Unknown
Charlie
Unknown
Unknown
This pattern is common. It keeps your code safe. You can also use optional chaining (?.) in modern JavaScript. But understanding the check is still important.
Best Practices Summary
Here are the key takeaways:
- Use
typeofwith anullcheck for quick filtering. - Use
instanceof Objectwhen you trust the realm. - Use
Object.prototype.toString.call()for the most accurate check. - Always check for
nullandundefined. - Remember that arrays and functions are objects too.
These methods will help you write safer code. They prevent type errors. They make your intentions clear.
Related Concepts
Understanding object types is part of a bigger picture. You should also learn about JavaScript Variable Interpolation Guide to combine strings and objects. And know how JavaScript Variable Shadowing can affect your object references.
Conclusion
Checking if a variable is an object is essential in JavaScript. The typeof operator is simple but has a null bug. The instanceof operator works well but includes arrays and functions. The Object.prototype.toString.call() method is the most accurate and reliable.
Choose the method that fits your needs. For most cases, the toString approach is best. It gives you a true plain object check. Use it to write clean, error-free code.
Practice these checks in your projects. They will become second nature. Happy coding!