In JavaScript, objects are collections of related data represented as key-value pairs. There are several ways to check whether a value is an object, including typeof, instanceof, the constructor property, Object.prototype.toString(), and Lodash's _.isObjectLike().
- typeof provides a simple type check but treats arrays, sets, and null as "object".
- instanceof checks whether a value is an instance of a specific constructor.
- constructor can help identify objects created using the Object constructor.
- Object.prototype.toString() provides a more specific type check.
- Lodash's _.isObjectLike() checks whether a value is a non-null object, including arrays.
Approach 1: Using typeof Operator
The typeof operator returns the type of a value. It is the simplest approach, but it cannot distinguish between ordinary objects, arrays, sets, and null.
Example: Checks the types of different JavaScript values using typeof.
let k = { Name: 'gfg', Country: 'India' };
let k0 = new Set();
let k1 = [1, 2, 3];
let k2 = "hello";
let k3 = null;
let k4 = undefined;
console.log(typeof k);
console.log(typeof k0);
console.log(typeof k1);
console.log(typeof k2);
console.log(typeof k3);
console.log(typeof k4);
Output
object object object string object undefined
[Approach 2]: Using instanceof Operator
The instanceof operator checks whether an object is an instance of a particular constructor. It returns true if the value belongs to that constructor's prototype chain.
Example: Checks whether different values are instances of the Object constructor.
let k = { Name: 'gfg', Country: 'India' };
let k0 = new Set();
let k1 = [1, 2, 3];
let k2 = "hello";
let k3 = null;
let k4 = undefined;
console.log(k instanceof Object);
console.log(k0 instanceof Object);
console.log(k1 instanceof Object);
console.log(k2 instanceof Object);
console.log(k3 instanceof Object);
console.log(k4 instanceof Object);
Output
true true true false false false
Approach 3: Using constructor Property
The constructor property refers to the constructor function associated with an object. Comparing it with Object can help identify ordinary objects.
Example: Checks whether the constructor of each value is Object.
let k = { Name: 'gfg', Country: 'India' };
let k0 = new Set();
let k1 = [1, 2, 3];
let k2 = "hello";
console.log(k.constructor === Object);
console.log(k0.constructor === Object);
console.log(k1.constructor === Object);
console.log(k2.constructor === Object);
Output
true false false false
Note: null and undefined do not have a constructor property, so accessing it directly on these values causes an error.
Approach 4: Using Object.prototype.toString() Method
The Object.prototype.toString() method returns a type tag for a value. Comparing it with "[object Object]" helps identify ordinary objects while excluding arrays, null, and primitive values.
Example: Checks whether a value is an ordinary object using Object.prototype.toString().
function isObject(value) {
return Object.prototype.toString.call(value) === "[object Object]";
}
console.log(isObject({}));
console.log(isObject([]));
console.log(isObject(null));
console.log(isObject("string"));
Output
true false false false
Approach 5: Using Lodash's _.isObjectLike() Function
Lodash's _.isObjectLike() function checks whether a value is not null and has a typeof value of "object". Therefore, it returns true for ordinary objects, arrays, sets, and other non-null objects.
Example: Checks whether values are object-like using Lodash.
const _ = require('lodash');
function isObjectLike(value) {
return _.isObjectLike(value);
}
console.log(isObjectLike({}));
console.log(isObjectLike([]));
console.log(isObjectLike(null));
Output:
true
true
false
Note: _.isObjectLike() considers arrays and other non-null objects as object-like, unlike Object.prototype.toString() with "[object Object]", which specifically identifies ordinary objects.