Suggest an editImprove this articleRefine the answer for “Can you omit the constructor”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**Yes, `constructor()` is optional: if you do not write one, JavaScript adds a default constructor.** For a class without `extends` that is an empty constructor which does nothing. For a class with `extends` the engine supplies `constructor(...args) { super(...args); }`, so every argument is simply forwarded to the parent. You only write a constructor by hand when you need your own initialisation logic or new fields, and in a derived class you must then call `super()` before the first use of `this`. ```javascript class Dog extends Animal {} // same as constructor(...args) { super(...args); } ``` **Key point:** a default constructor is always there, you are only deciding whether to override it.Shown above the full answer for quick recall.Answer (EN)Image**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 | 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 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.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.