How to get the Class Name of an Object in JavaScript Last Updated : 16 Nov, 2023 Comments Improve Suggest changes Like Article Like Report 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. Table of Content Using the constructor PropertyUsing the instanceof OperatorApproach 1: Using the constructor PropertyIn 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: ProductApproach 2: Using the instanceof OperatorBy 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:trueConclusion :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. Comment More infoAdvertise with us Next Article How to get the Class Name of an Object in JavaScript P pankaj_gupta_gfg Follow Improve Article Tags : JavaScript Web Technologies javascript-object 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 How To Get Element By Class Name In JavaScript ? When working with the DOM in JavaScript, selecting elements by their class names is a common task. JavaScript provides several methods to achieve this, whether we need to select one or multiple elements. In this article, we will cover different approaches to get elements by class name in JavaScript. 3 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 a Key Exists in JavaScript Object? Here are different ways to check a key exists in an object in JavaScript.Note: Objects in JavaScript are non-primitive data types that hold an unordered collection of key-value pairs. check a key exists in JavaScript object1. Using in Operator The in operator in JavaScript checks if a key exists in 2 min read How to create object properties in JavaScript ? JavaScript is built on an object-oriented framework. An object is a collection of properties, where each property links a key to a value. These properties are not in any specific order. The value of a JavaScript property can be a method (function). Object properties can be updated, modified, added, 4 min read Like