Bernard Aybouts - Blog - MiltonMarketing.com

FAQ: Issues using typeof bar === “object” to determine if bar is object?

FAQ

Approx read time: 2.2 min.

Determining if a Variable is an Object in JavaScript – JavaScript type checking

In JavaScript, checking the type of a variable to determine if it’s an object is a common task but it comes with certain pitfalls. Using typeof can sometimes lead to unexpected results.

Common Mistake with typeof

The typeof operator can misleadingly suggest that null is an object:

var bar = null;
console.log(typeof bar === "object"); // logs true!

This happens because, historically, null was considered a type of object due to a bug in JavaScript’s initial implementation. Even today, this behavior persists for compatibility reasons.

Correctly Checking for Objects

To avoid the above pitfall, it’s crucial to check not only that typeof returns "object" but also that the variable is not null:

var bar = null;
console.log(bar !== null && typeof bar === "object"); // logs false

Considering Functions and Arrays

Functions in JavaScript are objects but not in the typical sense. If you also want to consider functions as objects:

var bar = function() ;
console.log(bar !== null && (typeof bar === "object" || typeof bar === "function")); // logs true

Arrays are also technically objects. If you need to specifically exclude arrays when checking for objects:

var bar = [];
console.log(bar !== null && typeof bar === "object" && !Array.isArray(bar)); // logs false

More Precise Checks with Constructors

For a more robust check, especially to ensure the variable is exactly an object (not any subclass or other types), you can check the constructor:

var bar = ;
console.log(bar !== null && bar.constructor === Object); // logs true

This method will return true only if bar is explicitly an instance of Object.

Using jQuery for Checking

If you’re using jQuery, it provides utilities to simplify these checks, like $.isArray() for array detection:

var bar = [];
console.log(bar !== null && typeof bar === "object" && !$.isArray(bar)); // logs false

Simplified Array Checking in ES5

Lastly, ES5 introduced Array.isArray(), which simplifies array checking and includes its own null checks:

var bar = [];
console.log(Array.isArray(bar)); // logs true

Conclusion

By understanding the nuances of JavaScript’s type system and utilizing appropriate checks, you can avoid common mistakes and ensure your type checks are accurate and meaningful. This approach helps in writing more robust and maintainable JavaScript code.

Leave A Comment


About the Author: Bernard Aybout (Virii8)

Avatar of Bernard Aybout (Virii8)
I am a dedicated technology enthusiast with over 45 years of life experience, passionate about computers, AI, emerging technologies, and their real-world impact. As the founder of my personal blog, MiltonMarketing.com, I explore how AI, health tech, engineering, finance, and other advanced fields leverage innovation—not as a replacement for human expertise, but as a tool to enhance it. My focus is on bridging the gap between cutting-edge technology and practical applications, ensuring ethical, responsible, and transformative use across industries. MiltonMarketing.com is more than just a tech blog—it's a growing platform for expert insights. We welcome qualified writers and industry professionals from IT, AI, healthcare, engineering, HVAC, automotive, finance, and beyond to contribute their knowledge. If you have expertise to share in how AI and technology shape industries while complementing human skills, join us in driving meaningful conversations about the future of innovation. 🚀