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 Find Property Values in an Array of Object using if/else Condition in JavaScript ?
Finding property values in an array of objects using if/else condition is particularly useful when there is a collection of objects. Table of ContentUsing Array Find MethodUsing Array Filter MethodUsing For Of LoopUsing Array Map MethodUsing Array Reduce MethodUsing Array Some MethodUsing Array Find
5 min read
How to Check if the Response of a Fetch is a JSON Object in JavaScript?
In JavaScript, when making HTTP requests using the Fetch API, it's common to expect JSON data as a response from the server. However, before working with the response data, it's essential to verify whether it is indeed a JSON object or not. The below approaches can help you to check if the response
2 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
How to get property descriptors of an object in JavaScript ?
Here, we are going to discuss the property descriptors of an Object in JavaScript. The Object.getOwnPropertyDescriptor() method returns an object describing a specific property on a given object. A JavaScript object can be created in many ways and its properties can be called by using property descr
2 min read
How to get a key in a JavaScript object by its value ?
To get a key in a JavaScript object by its value means finding the key associated with a specific value in an object. Given an object with key-value pairs, you want to identify which key corresponds to a particular value, often for searching or data retrieval.How to get a key in a JavaScript object
4 min read
How to check whether an object exists in javascript ?
Checking whether an object exists in JavaScript refers to determining if a variable or property has been declared and contains a valid value. This helps avoid errors when accessing or manipulating objects that may be undefined, null, or not initialized properly.Here we have some common approaches to
3 min read