How to check if a variable is an object in JavaScript
Checking if a variable is an object in JavaScript requires careful consideration since arrays, null, and functions also return “object” from the typeof operator. As the creator of CoreUI, a widely used open-source UI library, I’ve implemented object type checking in countless JavaScript utilities and data validation functions. From my 25 years of experience in web development, the most reliable approach is to combine typeof with explicit checks for null and arrays. This method accurately identifies plain objects while excluding other object-like types.
Use typeof with null and array checks to accurately determine if a variable is a plain object.
function isObject(value) {
return typeof value === 'object' &&
value !== null &&
!Array.isArray(value)
}
// Usage examples
console.log(isObject({})) // true
console.log(isObject({name: 'John'})) // true
console.log(isObject([])) // false
console.log(isObject(null)) // false
console.log(isObject('string')) // false
console.log(isObject(42)) // false
console.log(isObject(function(){})) // false
The typeof operator returns “object” for objects, arrays, and null, so additional checks are needed. The value !== null check excludes null values, while !Array.isArray(value) excludes arrays. This combination accurately identifies plain objects created with object literals {} or the Object constructor. The function returns true only for actual object instances, making it reliable for data validation and type checking scenarios.
This is the same robust object checking approach we use in CoreUI components for props validation and data processing.
For more specific object type checking, you can also use Object.prototype.toString.call(value) === '[object Object]' to detect plain objects exclusively.



