- Instant help with your JavaScript coding problems

Get class name of an object in JavaScript

Question:
How to get class name of an object in JavaScript?
Answer:
class Animal {
    constructor(age) {
        this.age = age;
    }
}

class Cat extends Animal {

}

const animal = new Animal('Animal');
const cat = new Cat('Miau');

console.log(animal.constructor.name); // Animal
console.log(cat.constructor.name); // Cat
Description:

To get the class name of an object in JavaScript you can use the name property of the constructor function. If the object was created from a class using the new keyword then the result will be the real class name. However, if you read the name property of a constructor on an object that was created without the new operator, then you will Object as result.  

Share "How to get class name of an object in JavaScript?"
Tags:
get, object, classname, javascript
Technical term:
Get class name of an object in JavaScript