How to get the Class Name of an Object in JavaScript
Last Updated :
16 Nov, 2023
In this article, we will learn how we can get the class Name of an Object with multiple approaches in JavaScript.
In JavaScript, determining an object's class name can be done using multiple approaches. The constructor
property of an object reveals the function that created it, while the instanceof
operator checks if an object is an instance of a class. Additionally, employing Object.prototype.toString()
method with the object as an argument helps retrieve its class name in string form. These methods help in identifying the class of an object, help in handling different types of objects within JavaScript programs.
Approach 1: Using the constructor
Property
In JavaScript, the constructor property serves as a tag on an object, showing the function that formed it. It's a means to discover which constructor function was used to craft the object. By using object.constructor, you can simply finds the object's category without going into complexities, offering a clear path to recognize object types in JavaScript.
Example: In this example we will see how to find the Class Name of an Object using the constructor
Property in JavaScript.
JavaScript
class Product {
constructor(name, price) {
this.name = name;
this.price = price;
}
getClassName() {
return this.constructor.name;
}
}
const product = new Product("Book", 10);
console.log(product.getClassName());
Ouput:
Product
Approach 2: Using the instanceof
Operator
By utilizing the 'instanceof' check in JavaScript, we determine if an object belongs to a particular category, It's like asking if something is a part of a certain group. For example, employing 'instanceof' aids in understanding if an object aligns with a defined type, offering insights into its classification within the code.
Example: In this example we will see how to find the Class Name of an Object Using the instanceof
Operator in JavaScript.
JavaScript
class Product {
constructor(name) {
this.name = name;
}
// Check if the given object is an
// instance of the Product class.
isInstanceOfProduct(object) {
return object instanceof Product;
}
}
const product = new Product("Book");
// Check if the product object is an
// instance of the Product class.
const isInstanceOfProduct =
product.isInstanceOfProduct(product);
console.log(isInstanceOfProduct);
Output:
true
Conclusion :
In JavaScript, determining an object's class name is essential for understanding its type. Using the constructor property offers a straightforward method, revealing the function that created the object. Meanwhile, the instanceof operator provides a simple way to confirm if the object is an instance of a specific class. Both approaches contribute to identifying the object's class, aiding in clearer classification and better understanding of its type within the code.
Similar Reads
How to get the first key name of a JavaScript object ? In JavaScript, accessing the first key name of an object involves identifying the initial property defined within that object. This concept is useful when you need to interact with or manipulate object properties in a specific order, particularly when the sequence of properties is relevant.Here we h
2 min read
How to print the content of an object in JavaScript ? To print the content of an object in JavaScript we will use JavaScript methods like stringify, object.values and loops to display the required data. Let's first create a JavaScript Object containing some key-values as given below: JavaScript // Given Object const obj = { name: 'John', age: 30, city:
3 min read
How to get the size of a JavaScript object ? In this article, we will see the methods to find the size of a JavaScript object. These are the following ways to solve the problem: Table of Content Using Object.keys() methodUsing Object.objsize() methodUsing Object.entries() methodUsing Object.values() methodUsing Object.keys() methodWe can get t
2 min read
How to get all the methods of an object using JavaScript? In this article, we will learn how to get all the methods of an object using JavaScript. In JavaScript, we can get all the methods of an object by iterating over each object and checking if its property value is a function. An HTML document contains some methods and the task is to get all methods of
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 Access Array of Objects in JavaScript ? Accessing an array of objects in JavaScript is a common task that involves retrieving and manipulating data stored within each object. This is essential when working with structured data, allowing developers to easily extract, update, or process information from multiple objects within an array.How
4 min read