Sunday, December 29, 2024
Google search engine
HomeLanguagesJavascriptHow to check if the provided value is an object created by...

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




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

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments