How to check if the provided value is an object created by the Object constructor in JavaScript ?
Last Updated :
26 Jul, 2023
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
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
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));
// false (numbers are not objects)
Outputtrue
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
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;
console.log(
isObjectCreatedByObjectConstructor(object1));
// true
console.log(
isObjectCreatedByObjectConstructor(object2));
// true
console.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)
Outputtrue
true
false
false
false
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
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;
console.log(
isObjectCreatedByObjectConstructor(object1));
// true
console.log(
isObjectCreatedByObjectConstructor(object2));
// true
console.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)
Outputtrue
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
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;
console.log(isObjectCreatedByObjectConstructor(object1));
// true
console.log(isObjectCreatedByObjectConstructor(object2));
// true
console.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)
Outputtrue
true
false
false
false
Similar Reads
How to check if the value is primitive or not in JavaScript ? To check if the value is primitive we have to compare the value data type. As Object is the non-primitive data type in JavaScript we can compare the value type to object and get the required results. Primitive data types are basic building blocks like numbers and characters, while non-primitive data
3 min read
How to check if the provided value is of the specified type in JavaScript ? To check if the provided value is of the specified type in JavaScript, we have multiple approaches. Below are the approaches used to check if the provided value is of the specified type in JavaScript: Table of Content Using Object.is() Using TypeOf OperatorApproach: Using Object.is() Object.is() det
3 min read
How to check if a value is object-like in JavaScript ? In JavaScript, objects are a collection of related data. It is also a container for name-value pairs. In JavaScript, we can check the type of value in many ways. Basically, we check if a value is object-like using typeof, instanceof, constructor, and Object.prototype.toString.call(k). All of the ope
4 min read
How to check the type of a variable or object in JavaScript ? In this article, How to Check the Type of a Variable or Object in JavaScript? In JavaScript, the typeof operator is used to determine the typeof an object or variable. JavaScript, on the other hand, is a dynamically typed (or weakly typed) language. This indicates that a variable can have any type o
2 min read
How to check a JavaScript Object is a DOM Object ? Checking if a JavaScript object is a DOM object involves verifying whether the object represents an element or component within the Document Object Model (DOM) of an HTML or XML document. This can be done by checking if the object is an instance of Node, Element, or other specific DOM interfaces.Wha
2 min read