How to check if the provided value is an object created by the Object constructor in JavaScript ?

In this article, we will learn how to check if the provided value is an object created by the Object constructor in JavaScript. Almost all the values in JavaScript are objects except primitive values.
There are several methods that can be used to check if the provided value is an object created by the Object constructor in JavaScript.
- Using the instanceof operator
- Using the Object.prototype.toString.call() method
- Using Object.getPrototypeOf()
- Using a combination of type-checking and the constructor property
Approach 1: Using the instanceof operator
The instanceof 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: In this example, we are using the above-explained approach.
Javascript
| functionisObjectInstanceof(value) {    returnvalue instanceofObject;}// Test casesconst object1 = {};const object2 = newObject();const array = [];const date = newDate();const number = 42;console.log(isObjectInstanceof(object1)); console.log(isObjectInstanceof(object2)); console.log(isObjectInstanceof(array));   console.log(isObjectInstanceof(date));    console.log(isObjectInstanceof(number));  // false (numbers are not objects) | 
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: In this example, we are using the above-explained approach.
Javascript
| functionisObjectCreatedByObjectConstructor(value) {    returnObject.prototype.toString.call(value) ===         '[object Object]';}// Test casesconst object1 = {};const object2 = newObject();const array = [];const date = newDate();const number = 42;console.log(    isObjectCreatedByObjectConstructor(object1));// trueconsole.log(    isObjectCreatedByObjectConstructor(object2));// trueconsole.log(    isObjectCreatedByObjectConstructor(array));/* false (arrays are objects, but not    created by Object constructor) */console.log(    isObjectCreatedByObjectConstructor(date));/* false (dates are objects, but not    created by Object constructor) */console.log(    isObjectCreatedByObjectConstructor(number));// false (numbers are not objects) | 
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: In this example, we have the function isObjectCreatedByObjectConstructor that checks if the provided value is an object created by the Object constructor using Object.getPrototypeOf()
Javascript
| functionisObjectCreatedByObjectConstructor(value) {    returnObject.getPrototypeOf(value) ===         Object.prototype;}// Test casesconst object1 = {};const object2 = newObject();const array = [];const date = newDate();const number = 42;console.log(    isObjectCreatedByObjectConstructor(object1));// trueconsole.log(    isObjectCreatedByObjectConstructor(object2));// trueconsole.log(    isObjectCreatedByObjectConstructor(array));/* false (arrays are objects, but not    created by Object constructor) */console.log(    isObjectCreatedByObjectConstructor(date));/*false (dates are objects, but not   created by Object constructor)*/console.log(isObjectCreatedByObjectConstructor(number));// false (numbers are not objects) | 
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: In this example, we have the function isObjectCreatedByObjectConstructor that checks if the provided value is an object created by the Object constructor using a combination of checks.
Javascript
| functionisObjectCreatedByObjectConstructor(value) {    returnvalue !== null&& typeofvalue === 'object'        && value.constructor === Object;}// Test casesconst object1 = {};const object2 = newObject();const array = [];const date = newDate();const number = 42;console.log(isObjectCreatedByObjectConstructor(object1));// trueconsole.log(isObjectCreatedByObjectConstructor(object2));// trueconsole.log(isObjectCreatedByObjectConstructor(array));/* false (arrays are objects, but not     created by Object constructor) */console.log(isObjectCreatedByObjectConstructor(date));/* false (dates are objects, but not     created by Object constructor) */console.log(isObjectCreatedByObjectConstructor(number));// false (numbers are not objects) | 
true true false false false
 
				 
					


