How to Get Class Name in JavaScript
- Using the constructor property
- Using Object.prototype.toString
- Using instanceof operator
- Using the name property of the function
- Conclusion
- FAQ

When working with JavaScript, understanding how to retrieve the class name of an object can be crucial for debugging and development. Whether you’re developing a web application or a complex server-side solution, knowing how to effectively access class names can enhance your code’s readability and maintainability.
In this tutorial, we’ll explore various methods to get the class name of an object in JavaScript. We’ll cover simple techniques as well as more advanced options, ensuring you have a comprehensive understanding of this essential concept. By the end of this article, you will be equipped with practical examples and insights, enabling you to handle class names in your JavaScript projects seamlessly.
Using the constructor property
One of the most straightforward ways to get the class name of an object in JavaScript is by using the constructor
property. This property is a reference to the function that created the object instance. To retrieve the class name, you can access the name
property of the constructor.
Here’s a simple example:
class Animal {
constructor() {
this.type = 'Animal';
}
}
const dog = new Animal();
const className = dog.constructor.name;
console.log(className);
Output:
Animal
In this example, we define a class called Animal
. When we create a new instance of Animal
named dog
, we can access its class name by using dog.constructor.name
. This gives us the string “Animal”. This method is particularly useful because it is straightforward and works well with both built-in and user-defined classes.
Using Object.prototype.toString
Another effective method for obtaining the class name is by using the Object.prototype.toString
method. This method returns a string representing the object type. By calling it with the object you want to inspect, you can extract the class name from the resulting string.
Here’s how it works:
class Vehicle {
constructor() {
this.type = 'Vehicle';
}
}
const car = new Vehicle();
const className = Object.prototype.toString.call(car).slice(8, -1);
console.log(className);
Output:
Vehicle
In this code snippet, we define a class called Vehicle
. After creating an instance of Vehicle
named car
, we call Object.prototype.toString
with car
as an argument. The output of this method is a string that looks like “[object Vehicle]”. By slicing the string, we can isolate the class name, which in this case is “Vehicle”. This approach is particularly powerful because it can be used to determine the class name for a wide variety of object types, including those created from built-in JavaScript objects.
Using instanceof operator
The instanceof
operator is another way to get the class name indirectly by checking the type of an object against a specific class. While this method doesn’t directly return the class name, it can be used in conjunction with a conditional structure to determine the class type.
Here’s an example:
class Plant {
constructor() {
this.type = 'Plant';
}
}
const fern = new Plant();
let className;
if (fern instanceof Plant) {
className = 'Plant';
} else {
className = 'Unknown';
}
console.log(className);
Output:
Plant
In this example, we create a class called Plant
and instantiate an object named fern
. We then use the instanceof
operator to check if fern
is an instance of Plant
. If it is, we assign “Plant” to the className
variable. This method is useful when you want to verify an object’s type before performing operations on it, ensuring that you are working with the expected class.
Using the name property of the function
If you want to retrieve the class name of a function or a class, you can directly access the name
property of the function itself. This method is particularly useful for functions defined using the function
keyword or ES6 class syntax.
Here’s an example:
function Animal() {
this.type = 'Animal';
}
const animalClassName = Animal.name;
console.log(animalClassName);
Output:
Animal
In this example, we define a function called Animal
. By accessing Animal.name
, we directly retrieve the string “Animal”. This method is straightforward and efficient when you need the class name of a function or class without needing to instantiate it.
Conclusion
In this tutorial, we’ve explored various methods to retrieve the class name of an object in JavaScript. From using the constructor
property to leveraging Object.prototype.toString
and the instanceof
operator, each method offers unique advantages depending on your specific use case. Understanding these techniques can significantly improve your coding efficiency and debugging processes. As you continue to work with JavaScript, incorporating these methods into your toolkit will help you write clearer and more maintainable code.
FAQ
-
How can I get the class name of an object in JavaScript?
You can retrieve the class name using the constructor property, Object.prototype.toString, or the instanceof operator. -
What is the constructor property in JavaScript?
The constructor property is a reference to the function that created the instance of an object, allowing you to access the class name. -
Is it possible to get the class name of built-in objects?
Yes, you can use the Object.prototype.toString method to get the class name of built-in objects as well as user-defined classes.
-
Can I use the name property of a function to get its class name?
Yes, you can directly access the name property of a function or class to retrieve its name. -
What is the advantage of using the instanceof operator?
The instanceof operator allows you to check if an object is an instance of a specific class, which can be useful for type validation.