How to get the Class Name of an Object in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 2, 2024
3 min

banner

# Get the Class Name of an Object in JavaScript

Access the name property on the object's constructor to get the class name of the object, e.g. obj.constructor.name.

The constructor property returns a reference to the constructor function that created the instance object.

index.js
class Person {} const p1 = new Person(); console.log(p1.constructor.name); // ๐Ÿ‘‰๏ธ Person console.log(Person.name); // ๐Ÿ‘‰๏ธ Person

get class name of object

The code for this article is available on GitHub

We accessed the name property on the Object.constructor property.

The Object.constructor property returns a reference to the constructor function from which the object was created.

index.js
class Person {} const p1 = new Person(); // โœ… The function (class) that created the object console.log(p1.constructor); // ๐Ÿ‘‰๏ธ [class Person] // โœ… The class name of the object (string) console.log(p1.constructor.name); // ๐Ÿ‘‰๏ธ "Person"

# Get the Class Name of an Object by creating a method on the class

An alternative approach is to create a method on the class to get the class name of the object.

index.js
class Person { getClassName() { return this.constructor.name; } } const p1 = new Person(); const className = p1.getClassName(); console.log(className); // ๐Ÿ‘‰๏ธ Person

get class name of object by creating method on the class

The code for this article is available on GitHub

If you need to access the Object.constructor.name property often, create a method that hides it.

All objects (other than ones created using Object.create(null)) have a constructor property.

Objects that are created without a constructor function have a constructor property that points to the main Object constructor for the specific type.

index.js
console.log({}.constructor.name); // ๐Ÿ‘‰๏ธ Object console.log([].constructor.name); // ๐Ÿ‘‰๏ธ Array

# Accessing the name property directly on the class

You can also access the name property directly on the class to get its name as a string.

index.js
class Person {} console.log(Person.name); // ๐Ÿ‘‰๏ธ Person

# Checking if an object was created using a specific class

You can check if an object was created using a specific class in multiple ways.

The obj.constructor.name === Person.name option should be preferred when you minify your code for production.

index.js
class Person {} const p1 = new Person(); if (p1.constructor.name === 'Person') { // ๐Ÿ‘‡๏ธ this runs console.log("The object's constructor is Person"); } // -------------------------------------------------- // โœ… If you minify your code for production, do this if (p1.constructor.name === Person.name) { // ๐Ÿ‘‡๏ธ this runs console.log("The object's constructor is Person"); }

checking if object was created using specific class

The code for this article is available on GitHub

The first if statement accesses the constructor.name property to get the class name of the object and compares it to the string "Person".

index.js
class Person {} const p1 = new Person(); if (p1.constructor.name === 'Person') { // ๐Ÿ‘‡๏ธ this runs console.log("The object's constructor is Person"); }

This would work most of the time, however, if possible, it is better to compare the object's class name to Person.name.

index.js
class Person {} const p1 = new Person(); // โœ… If you minify your code for production, do this if (p1.constructor.name === Person.name) { // ๐Ÿ‘‡๏ธ this runs console.log("The object's constructor is Person"); }

If you minify your code for production, the name of the class might change. This isn't an issue when you access the name property on the class in the comparison.

# Get the class name of an object by defining a static method

You can also define a static method that you can access directly on the class to get the class name of an object.

index.js
class Person { static getClassName() { return 'Person'; } getClassName() { return Person.getClassName(); } } const p1 = new Person(); const className = p1.getClassName(); console.log(className); // ๐Ÿ‘‰๏ธ Person console.log(Person.getClassName()); // ๐Ÿ‘‰๏ธ Person

get class name of object by defining static method

The code for this article is available on GitHub

The static keyword defines a static method or field. Static methods cannot be directly accessed on instances of the class and must be accessed on the class itself.

Our static method returns the string Person and our instance method calls the static method to return the same value.

If you need to check if an object is an instance of a class at runtime, use the instanceof operator.

index.js
class Person {} const p1 = new Person(); class Animal {} console.log(p1 instanceof Person); // ๐Ÿ‘‰๏ธ true console.log(p1 instanceof Animal); // ๐Ÿ‘‰๏ธ false

The instanceof returns true if the prototype property of a constructor appears in the prototype chain of an object and false otherwise.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev