Skip to main content

Is it possible not to define a constructor?

What Happens if There Is No Constructor

If you don't define a constructor(), JavaScript automatically creates one by default.

Example:

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

Under the hood, the engine creates a constructor like this:

javascript
class User { constructor(...args) { super(...args); // if there is a parent class } }

If a Class Does Not Inherit (No extends)

Then the standard "empty" constructor simply does nothing:

javascript
class User {} // equivalent to: class User { constructor() {} }

If a Class Inherits From Another (extends Is Present)

Then a constructor is required only if you want to add logic and use this. If there isn't one, the engine adds a constructor itself 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

Important: If You Add a Constructor in the Subclass Yourself

You must 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 don't call it:

javascript
constructor(name, breed) { this.breed = breed; // ReferenceError: Must call super constructor before accessing 'this' }

Summary

SituationDo You Need to Write constructor()?
Class without inheritanceNot needed
Class inherits another one but adds no new behaviorNot needed
Class inherits and adds its own fieldsNeeded (and must call super())
Logic must run when the instance is created (e.g., initialization, validation)Needed

Short Answer

Interview ready
Premium

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