Get the class name of the object in JavaScript

In this tutorial, we will see how to get the class name of the object in JavaScript, that is created as an instance of a class or a function that is invoked as constructor.

For example, let’s say we have this class Vehicle.

class Vehicle {
  ...
}

const car = new Vehicle();

And the object car is the instance of the Vehicle, now if the object car is passed as a reference and we want to check from which it is derived from, we can use its constructor property.

console.log(car.constructor.name);
// Vehicle

object.constructor.name returns the string value of the Class name it is derived from, you can also use the constructor property to check if the object is the instance of the class or not.

class Vehicle {
  ...
}

const car = new Vehicle();

console.log(car.constructor === Vehicle);
// true