Skip to main content

How to Get the Class Name of an Object in JavaScript

In object-oriented programming, you sometimes need to inspect an object to find out which class it was created from. This is useful for debugging, logging, or implementing conditional logic based on an object's type.

This guide will teach you the modern, standard method for getting the class name of an object instance. You will also learn the important distinction between getting the name as a string and checking if an object is an instance of a specific class.

Goal 1: Get the Class Name as a String

If you need to get the name of the class that an object was instantiated from as a string (e.g., "Person"), the most direct way is to use the constructor.name property.

The logic:

  • Every object instance has a constructor property that points to the class or function that created it.
  • Every function and class has a name property that returns its name as a string.

By chaining them (object.constructor.name), we can get the class name.

For example, we have an object instance and we want to get the name of its class for logging or debugging.

// Problem: How to get the string "Person" from this instance?
class Person {}
const p1 = new Person();

Recommended Solution:

class Person {}
const p1 = new Person();

// Access the .name property on the object's constructor
const className = p1.constructor.name;

console.log(className); // Output: "Person"
note

This is the standard and most readable way to get the class name of an instance.

Goal 2: Check if an Object is an Instance of a Class

A more common and robust requirement is not to get the name, but to verify that an object is an instance of a specific class. For this task, you should not compare strings. The correct tool is the instanceof operator.

For example, we want to check if a variable p1 holds an instance of the Person class.

class Person {}
class Animal {}
const p1 = new Person();

The recommended solution uses the instanceof operator that checks the object's entire prototype chain and returns a true boolean.

class Person {}
class Animal {}
const p1 = new Person();

// This is the correct and robust way to check an object's type.
if (p1 instanceof Person) {
console.log('✅ p1 is an instance of Person.');
} else {
console.log('⛔️ p1 is NOT an instance of Person.');
}

console.log(p1 instanceof Animal); // Output: false

Output:

✅ p1 is an instance of Person.
false
note

This is superior to a string comparison (p1.constructor.name === 'Person') because it is more reliable and also works correctly with inheritance (a subclass instance is also an instance of its parent class).

A Note on Production Code and Minification

A common "gotcha" with constructor.name is that code minifiers, which are often used in production builds to reduce file size, may rename your classes.

The problem:

  • In Development: p1.constructor.name might be "Person".
  • In Production (after minification): The Person class might be renamed to a, so p1.constructor.name would be "a".

This will break any code that relies on a hardcoded string comparison, like if (p1.constructor.name === 'Person').

The solution:

  • For type-checking, always use instanceof. It is not affected by minification.
  • If you must compare names, compare them dynamically: p1.constructor.name === Person.name. This is safer because if Person is minified to a, Person.name will also be "a", and the comparison will still work.

Conclusion

Getting information about an object's class is a simple task if you choose the right tool for your goal.

  • To get the class name as a string for logging or debugging, the best method is object.constructor.name.
  • To check if an object is an instance of a specific class, the definitive and most robust method is the instanceof operator. It is safer, more flexible, and unaffected by code minification.