What is a class constructor?
A constructor is a special class method in JavaScript that is called automatically when a new object is created via new.
It is used for initializing the object: assigning initial values to properties, calling methods, and so on.
Let's look at this in detail:
What a Constructor Is
Example:
class User {
constructor(name, age) {
this.name = name; // create an object property
this.age = age;
}
sayHi() {
console.log(`Hi, my name is ${this.name}, I am ${this.age}`);
}
}
const user = new User('Oleh', 25);
user.sayHi(); // Hi, my name is Oleh, I am 25The
constructor()method fires at the momentnew User(...)is called, and it is used to set the object's initial state.
How the Constructor Works Under the Hood
When you write:
const user = new User('Oleh', 25);the following happens:
- A new empty object
{}is created. - This object is linked to the
User.prototypeprototype. constructor('Oleh', 25)is called inside the class.- Code runs inside the constructor, usually
this.name = .... - The created object (
this) is returned.
In other words,
newcreates the object itself and returns it, you don't need to writereturn.
Important Features
1. The constructor is called automatically with new
new User('Alex', 30); // calls constructor()2. A class can have only one constructor
class Test {
constructor() {}
constructor() {} // Error: Duplicate constructor
}If you need different behavior, you can use default parameters or conditions inside the constructor.
3. The constructor can be omitted
If you don't define a constructor, one is created by default:
class User {
sayHi() {
console.log('Hi!');
}
}
const user = new User();
user.sayHi(); // Hi!Under the hood, the browser adds this itself:
constructor() {}4. The constructor in derived classes
When a class extends another one via extends,
it must call the parent's constructor with super() before using this.
class Person {
constructor(name) {
this.name = name;
}
}
class Student extends Person {
constructor(name, group) {
super(name); // call the parent's constructor
this.group = group;
}
}
const s = new Student('Oleh', 'Frontend-2025');
console.log(s.name); // Oleh
console.log(s.group); // Frontend-2025If
super()is not called, you get an error:ReferenceError: Must call super constructor in derived class before accessing 'this'.
5. The constructor can return a different object
Normally a constructor has no return,
but if it explicitly returns an object, that object replaces the created instance:
class Example {
constructor() {
return { custom: true };
}
}
const obj = new Example();
console.log(obj); // { custom: true }If it returns something other than an object (for example, a number or a string), that return value is ignored.
Example: A Class With Logic in the Constructor
class Product {
constructor(name, price) {
this.name = name;
this.price = price > 0 ? price : 0; // validation
}
getInfo() {
return `${this.name}: $${this.price}`;
}
}
const p1 = new Product('Shirt', 2500);
const p2 = new Product('Coat', -100);
console.log(p1.getInfo()); // Shirt: $2500
console.log(p2.getInfo()); // Coat: $0A constructor is often used for validation and data preparation.
Quick Comparison: constructor vs Regular Methods
| Feature | constructor() | Regular Methods |
|---|---|---|
Called automatically with new | Yes | No |
| Can only have one | Yes | No |
| Used for object initialization | Yes | Sometimes |
| Can return a different object | Yes | No |
Accessible via this | Yes | Yes |
Summary
A class constructor is a special method that is called when an object is created via
new, and it is used for initializing properties and preparing the instance.
Formula to remember:
class X {
constructor(...) {
this.properties = ...
}
}
const obj = new X(...);Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.