Skip to main content

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 extends gets an empty constructor that does nothing.
  • A class with extends gets constructor(...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 of this.

Quick example

javascript
class User { greet() { console.log("Hi!"); } } const u = new User(); u.greet(); // Hi!

Under the hood the engine creates roughly this constructor:

javascript
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.

javascript
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().

javascript
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); // Rex

That 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.

javascript
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:

javascript
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

SituationIs constructor() needed
Class without inheritanceNot needed
Class inherits another but adds no new behaviourNot needed
Class inherits and adds its own fieldsNeeded, 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 of super(name) or super(...args).
  • Touching this or class fields before super(): that is a ReferenceError, not undefined.
  • 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 ready
Premium

A concise answer to help you respond confidently on this topic during an interview.