There isn’t a straightforward way to retrieve your class name in JavaScript. However, we can know how to get the class name of an object in JS by combining a few accessible methods.
Get the class name of an object in JavaScript
Method 1: Built-in constructor name
In JavaScript, every object has a constructor named after the class or object itself.
Object.prototype.constructor:
The constructor property gives back a pointer to the Object constructor method, which generates a new instance of the object.
It should be noted that the value of this attribute refers to the function itself, not just a string with the function’s name in it.
Function.prototype.name:
The name attribute, which displays the read-only name supplied for a function (as an object).
It is possible for this “name” to be ‘anonymous’ or ” (empty string).
We’ll run our code in the following way:
class Something {} NewThing = new Something(); console.log(NewThing.constructor.name);
Output
Something
You can always make a function within your class as follows:
class Something { getName() { return this.constructor.name; } } NewThing = new Something(); console.log(NewThing.getName());
Additionally, you can get the name of the class directly as follows:
class Something{} console.log(Something.name);
They will give the same result.
Output
Something
Method 2: Without using function name
Alternately, the function.prototype.name
method might not work with all browser versions. This strategy could work with browsers from before ES2015. Even when we use object.prototype.constructor
, we can still tell what class we are using.
class Something {} NewThing = new Something(); console.log(NewThing.constructor.toString());
Output
[class Something]
The written text is not an object that can be filtered. Instead of a string, prototype.constructor
delivers a function replica. Since toString()
stringifies every element of the object you call, using it alone might result in a prolonged string.
class Something {} NewThing = new Something(); console.log(NewThing.constructor.toString().match(/ (\w+)/)[1]);
Output
Something
Code explanation:
First, the object.prototype.constructor
delivers a reference to the constructor of the Something
class, which created Newthing
.
Next, a string that represents the function is returned.
Class Something{ }
We only pick the first occurrence that fits the pattern when we filter out the class name using the match()
function, which starts from the blank space, hence [1].
Summary
The later versions of browsers that support the latest ES patches will be more likely to have a direct approach to how to get the class name of an object in JavaScript. Otherwise, we have covered the uses and understanding of prototype functions and properties of objects in JavaScript to mix and match them well.
Maybe you are interested:
- Check if an object is empty in JavaScript
- Check if a key exists in a javaScript object
- Check if an Object Property is Undefined in JavaScript
- Remove all Undefined Values from an Object in JavaScript

My name is Tom Joseph, and I work as a software engineer. I enjoy programming and passing on my experience. C, C++, JAVA, and Python are my strong programming languages that I can share with everyone. In addition, I have also developed projects using Javascript, html, css.
Job: Developer
Name of the university: UTC
Programming Languages: C, C++, Javascript, JAVA, python, html, css