The Wayback Machine - https://web.archive.org/web/20240708012928/https://www.geeksforgeeks.org/how-to-check-if-the-provided-value-is-an-object-created-by-the-object-constructor-in-javascript/
Open In App

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

Last Updated : 26 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




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)


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: 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)


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: 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)


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: 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)


Output

true
true
false
false
false



Similar Reads

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() determines whether two values are the same value. Two
3 min read
How to replace the names of multiple object keys with the values provided using JavaScript ?
The following approach covers how to replace the names of multiple object keys with the values provided by the user using JavaScript. Problem Statement: You are given an object which contains different key-value pairs in which key symbolizes the property and value itself is known as the property value, and you need to change one or more key's origi
4 min read
How to create a function that invokes the provided function with its arguments transformed in JavaScript ?
In programming, functions are used to reduce the effort of writing the same instance of code repeatedly. In this article let us see how we can create a function that invokes the provided function with its arguments transformed in JavaScript. In this article, the function transformer invokes the function scaling with its arguments, where the functio
2 min read
How to create a function that invokes each provided function with the arguments it receives using JavaScript ?
In this article, we will see how to create a function that invokes each provided function with the arguments it receives using JavaScript. It helps in reducing the effort of creating the same instance of code again and again. In this article let us see how to create a function that invokes each provided function with the arguments it receives and r
3 min read
JavaScript Object.prototype.constructor Property
The constructor property returns a reference to the object constructor function that has created the instance of an object. The value of the constructor is not a string containing the function's name, but it is a reference to the function itself. Syntax: Object.constructor Return Value: It is a reference to the object of constructor. Example 1: Bel
1 min read
How Check if object value exists not add a new object to array using JavaScript ?
In this tutorial, we need to check whether an object exists within a JavaScript array of objects and if they are not present then we need to add a new object to the array. We can say, every variable is an object, in Javascript. For example, if we have an array of objects like the following. obj = { id: 1, name: ''Geeks1" }, { id: 2, name: '"Geeks2"
3 min read
How to handle events in dynamically created elements in jQuery ?
When we want to bind any event to an element, normally we could directly bind to any event of each element using the on() method. Example 1: This example using jQuery on() method to add paragraph element dynamically. <!DOCTYPE html> <html> <head> <title> How to handle events in dynamically created elements in jQuery? </ti
2 min read
Why does SASS cache folder is created ?
CSS is used to style and design web pages and in general improve the User Interface and Experience of a web page. The Development of such CSS design templates involves making the resources for CSS and then writing the code after which testing is done. Testing involves compiling the program before executing it. This compilation of CSS resources and
2 min read
What is the difference between created and mounted event in VueJS?
VueJS is a model- view-view-model JavaScript framework for building user interfaces and single-page applications. It has several lifecycle hooks (not more than 8). In this article, we are going to differentiate two types of events that are part of the lifecycle of a component. CreatedMounted And among both of them, mounted hooks are also known as m
2 min read
How to put the text inside of a created icon?
The task is to put a text inside of a created icon. This post will go over some of the different ways in which you can put a text inside/over of a created icon/image(s). All of the discussed solutions follow a different presentation based on the same base idea i.e, to write/put the text over the image/icon make the image/icon an background image/ic
3 min read
three90RightbarBannerImg