Retrieve the Class Name at Runtime in JavaScript and Node.js

Sometimes you need to retrieve the name of a class in JavaScript. This is helpful for debug messages or when using the class name as an identifier. Retrieving the class name in JavaScript is different for static and instance methods.

This tutorial walks you through the individual method types and shows you how to access the class name.

Node.js Series Overview

  1. Retrieve the Class Name at Runtime in JavaScript and Node.js
  1. How to Check if an Object is Empty in JavaScript or Node.js (Coming soon)

In Static Methods

Retrieving the class name in static methods is pretty straightforward. Because JavaScript represents classes as functions, you can access the name property to retrieve the name of the class.

Retrieve the class name in static methods by either using this.name or Class.name:

class User {  
  /**
   * Returns the string 'User'.
   *
   * @returns {String}
   */
  static className() {
    return this.name

    // is the same as
    // return User.name
  }
}

console.log(  
  User.className()
)
// 'User'

Be Aware of a Static “name“ Property in Classes

There’s a pitfall you should be aware of. When defining a static name property in your class, it will overwrite the name property of the class.

class User {  
  /**
   * Returns the name.
   *
   * @returns {String}
   */
  static get name () {
    return 'Marcus'
  }

  /**
   * Returns the string 'Marcus' because the static `name`
   * property overwrites the name property of the class.
   *
   * @returns {String}
   */
  static className() {
    return this.name
  }
}

console.log(  
  User.className()
)
// 'Marcus'

It’s recommended to use a static name property with caution when working with JavaScript’s classes. It can be a helpful way to customize the class name to your own needs.

In Instance Methods

You can access the class name in instance methods using either the constructor or the class itself. The constructor is available via this.constructor and provides the class properties:

class User {  
  /**
   * Returns the string 'User'.
   *
   * @returns {String}
   */
  className() {
    return this.constructor.name

    // is the same as
    // return User.name
  }
}

console.log(  
  User.className()
)
// 'User'

Text

Having a Static “name“ Property in Classes

Please notice, having a static name property on your class overrides the actual class name:

class User {  
  /**
   * Returns the name.
   *
   * @returns {String}
   */
  static get name () {
    return 'Marcus'
  }

  /**
   * Returns the string 'Marcus' because the static `name`
   * property overwrites the name property of the class.
   *
   * @returns {String}
   */
  static className() {
    return this.constructor.name
  }
}

console.log(  
  User.className()
)
// 'Marcus'

That’s it about accessing the class name from static and instance methods. If you know of another way to access the class name, please share it in the comments below!

Explore the Library

Find interesting tutorials and solutions for your problems.