Can you omit the constructor
Yes, you can leave the constructor out of a class: JavaScript automatically creates a default one. The shape of that implicit constructor depends on whether the class inherits another class with extends.
Theory
TL;DR
- If no
constructor()is written, the engine adds one for you. - A class without
extendsgets an empty constructor that does nothing. - A class with
extendsgetsconstructor(...args) { super(...args); }, so arguments are forwarded to the parent transparently. - You need your own constructor when there are new fields or initialisation logic.
- In a derived class your own constructor must call
super()before the first use ofthis.
Quick example
class User {
greet() {
console.log("Hi!");
}
}
const u = new User();
u.greet(); // Hi!Under the hood the engine creates roughly this constructor:
class User {
constructor(...args) {
super(...args); // only if there is a parent class
}
}A class without inheritance
If a class has no extends, the default "empty" constructor simply does nothing.
class User {}
// equivalent to:
class User {
constructor() {}
}Both forms give the same result: new User() creates an empty object whose prototype is User.prototype. The class methods still work, because they live on the prototype rather than being created in the constructor.
A class that inherits another
If a class inherits another one with extends, a constructor is required only when you want to add your own logic and use this. If there is none, the engine adds a constructor that calls super().
class Animal {
constructor(name) {
this.name = name;
}
}
class Dog extends Animal {}
// Equivalent to:
class Dog extends Animal {
constructor(...args) {
super(...args);
}
}
const dog = new Dog("Rex");
console.log(dog.name); // RexThat is exactly why new Dog("Rex") fills in name correctly even though Dog contains no constructor code at all.
If you do write a constructor in the child
Then you are obliged to call super() before using this, otherwise you get an error.
class Animal {
constructor(name) {
this.name = name;
}
}
class Dog extends Animal {
constructor(name, breed) {
// super() must come first
super(name);
this.breed = breed;
}
}If you do not call it:
constructor(name, breed) {
this.breed = breed; // ReferenceError: Must call super constructor before accessing 'this'
}The reason is that in a derived class the this object is created by the parent constructor, so before super() runs there is simply nothing to refer to.
When a constructor is needed
| Situation | Is constructor() needed |
|---|---|
| Class without inheritance | Not needed |
| Class inherits another but adds no new behaviour | Not needed |
| Class inherits and adds its own fields | Needed, and must call super() |
| You must run logic when an instance is created (initialisation, validation) | Needed |
Common mistakes
- Writing an empty
constructor() {}"just in case": that is exactly what the engine does anyway, so it is only noise in the code. - Writing
constructor(name) { super(); }in the child and losing the arguments, instead ofsuper(name)orsuper(...args). - Touching
thisor class fields beforesuper(): that is aReferenceError, notundefined. - Thinking that without a constructor the class methods stop working. Methods live on the prototype and do not depend on the constructor.
- Declaring a constructor in the child only to call
super(...args): the default constructor already does that.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.