Explain Codes LogoExplain Codes Logo

Check if a value is an object in JavaScript

javascript
object-checking
type-detection
libraries
Nikita BarsukovbyNikita Barsukov·Aug 19, 2024
TLDR

The simplest way to check if a value is an object strictly (excluding null, an array, or a function) involves a short combination of checks:

function isPlainObject(value) { // In JavaScript, null is also defined as an 'object'. // Thus, check if the value is not null and if it is indeed an 'object' // P.S. Don't take JavaScript too 'literal' on the facts. ;) return !!value && typeof value === 'object' && value.constructor === Object; } console.log(isPlainObject({})); // true, a pure object console.log(isPlainObject([])); // false, mutants are not humans console.log(isPlainObject(null)); // false, null will never stop if it's not null console.log(isPlainObject(() => {})); // false, arrows do not always hit the target properly.

This neat little isPlainObject function will only return true if an object is genuinely created using {} or new Object.

Getting deeper in the rabbit hole

When handling JavaScript, things may not always be as they seem. Here's diving deeper into the object checking territory with more refined checks and explanations:

Ditching arrays and functions from the party

Despite functions and arrays secretly wish to be objects, we won't grant their wish:

function isObjectButNotArray(value) { // An array is a party crasher pretending to be an object. // We won't let it fool us. return typeof value === 'object' && !Array.isArray(value) && value !== null; } console.log(isObjectButNotArray({})); // true, pure objects always get the invite console.log(isObjectButNotArray([])); // false, party crasher alert console.log(isObjectButNotArray(null)); // false, no value, no entry console.log(isObjectButNotArray(() => {})); // false, transformers are not real objects

Null, the imposter, and the wrapper objects

Null, disguised as an object, continues to fool us all, and the wrapper objects just add to the complexity:

function isActualObject(value) { // Let's identify true objects and kick out the null imposter return value !== null && (typeof value === 'object' || typeof value === 'function'); }

Also, keep in mind about primitive wrapper objects. A deceptive new String('text') will return an object, not a primitive string.

Accurate types with a bit of charm

The Object.prototype.toString method can precisely nail down types. Use it to have distinct, reliable results:

function preciseTypeOf(value) { // Here we play the detective and call out each type. return Object.prototype.toString.call(value); // Announces [object Type] }

The Underscore library offers an even robust function _.isObject for this cumbersome task:

console.log(_.isObject({})); // true. Underscore never lies. ;)

Be cautious with instanceof Object, though. It's stingy and fails to recognize objects using Object.create(null).

Cracking the JavaScript code

Language quirks and misleading operators

More often than not, typeof and instanceof become tricksters, and null keeps pretending to be an object. Outsmart them with a more cautious approach.

The ever-evolving JavaScript world

JavaScript engine bugs, especially V8, posed object check issues, but recent improvements helped get rid of them to a large extent.

Libraries to the rescue!

Libraries like Underscore and Lodash fight these oddities with their isObject methods, providing a more consistent behavior across environments.