In JavaScript, objects can be created in multiple ways. To ensure a value is specifically created using the Object constructor, certain checks can be applied.
- Use
value.constructor === Objectto verify the constructor. - Use
Object.prototype.toString.call(value) === '[object Object]'for a reliable check. - Excludes arrays, functions, and other non-plain objects.
- Helps distinguish plain objects from instances of custom classes or built-in types.
There are several methods that can be used to check if a value is created by the Object constructor in JavaScript.
[Approach -1] Using the instanceof operator
The instance of operator tests whether an object's prototype chain contains the prototype property of a constructor. It checks if the provided value is an instance of the specified constructor or its derived classes.
Example
function isObjectInstanceof(value) {
return value instanceof Object;
}
// Test cases
const object1 = {};
const object2 = new Object();
const array = [];
const date = new Date();
const number = 42;
console.log(isObjectInstanceof(object1));
console.log(isObjectInstanceof(object2));
console.log(isObjectInstanceof(array));
console.log(isObjectInstanceof(date));
console.log(isObjectInstanceof(number));
Output
true true true true false
[Approach - 2] Using the Object.prototype.toString.call() method
Using Object.prototype.toString.call(), it retrieves the internal [[Class]] property, and comparing it to "[object Object]" verifies if the provided value is an object created by the Object constructor.
Example
function isObjectCreatedByObjectConstructor(value) {
return Object.prototype.toString.call(value) === '[object Object]';
}
// Test cases
const object1 = {};
const object2 = new Object();
const array = [];
const date = new Date();
const number = 42;
// true
console.log(isObjectCreatedByObjectConstructor(object1));
// true
console.log(isObjectCreatedByObjectConstructor(object2));
// false (arrays are objects, but not
// created by Object constructor)
console.log(isObjectCreatedByObjectConstructor(array));
// false (dates are objects, but not
// created by Object constructor)
console.log(isObjectCreatedByObjectConstructor(date));
// false (numbers are not objects)
console.log(isObjectCreatedByObjectConstructor(number));
Output
true true false false false
[Approach - 3] Using Object.getPrototypeOf()
Using Object.getPrototypeOf(), we can retrieve the prototype of the provided object, and if it matches Object.prototype, we can verify if the value is an object created by the Object constructor.
Example
function isObjectCreatedByObjectConstructor(value) {
return Object.getPrototypeOf(value) ===
Object.prototype;
}
// Test cases
const object1 = {};
const object2 = new Object();
const array = [];
const date = new Date();
const number = 42;
// true
console.log(isObjectCreatedByObjectConstructor(object1));
// true
console.log(isObjectCreatedByObjectConstructor(object2));
// false (arrays are objects, but not
// created by Object constructor)
console.log(isObjectCreatedByObjectConstructor(array));
// false (dates are objects, but not
// created by Object constructor)
console.log(isObjectCreatedByObjectConstructor(date));
// false (numbers are not objects)
console.log(isObjectCreatedByObjectConstructor(number));
Output
true true false false false
[Approach - 4] Using a combination of type-checking and the constructor property
This approach involves multiple checks: it ensures that the value is an object (not null or other primitive types) and that its constructor is the Object constructor.
Example
function isObjectCreatedByObjectConstructor(value) {
return value !== null && typeof value === 'object'
&& value.constructor === Object;
}
// Test cases
const object1 = {};
const object2 = new Object();
const array = [];
const date = new Date();
const number = 42;
// true
console.log(isObjectCreatedByObjectConstructor(object1));
// true
console.log(isObjectCreatedByObjectConstructor(object2));
// false (arrays are objects, but not
// created by Object constructor)
console.log(isObjectCreatedByObjectConstructor(array));
// false (dates are objects, but not
// created by Object constructor)
console.log(isObjectCreatedByObjectConstructor(date));
// false (numbers are not objects)
console.log(isObjectCreatedByObjectConstructor(number));
Output
true true false false false