Suggest an editImprove this articleRefine the answer for “Multiple constructors in a class”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)No, a class in JavaScript can have only **one** `constructor()`; trying to declare a second one results in `SyntaxError: A class may only have one constructor`. **Key point:** the same flexibility that an overloaded signature would give is achieved differently, by checking arguments inside a single constructor, using a parameter object, static factory methods (`User.fromName()`), or inheritance with a separate constructor in each subclass.Shown above the full answer for quick recall.Answer (EN)Image## 1. Why You Can't Have Multiple Constructors If you try to declare two constructors in the same class, you get a syntax error: ```javascript class User { constructor(name) { this.name = name; } // Error: duplicate constructor declaration constructor(name, age) { this.name = name; this.age = age; } } ``` > `SyntaxError: A class may only have one constructor` --- ## 2. How to Properly Simulate Multiple Constructors Although there's no direct overloading, in JS you can implement a **flexible constructor** in several ways: --- ### Option 1: Checking Arguments Inside a Single Constructor ```javascript class User { constructor(name, age) { if (typeof age === "undefined") { this.name = name; this.age = "unknown"; } else { this.name = name; this.age = age; } } } const u1 = new User("Alice"); const u2 = new User("Alex", 25); console.log(u1); // { name: "Alice", age: "unknown" } console.log(u2); // { name: "Alex", age: 25 } ``` This approach works like "overloading by argument count". --- ### Option 2: Use a **Parameter Object** When there are many arguments, it's often more convenient to accept an object: ```javascript class User { constructor({ name, age = null, email = null } = {}) { this.name = name; this.age = age; this.email = email; } } const u1 = new User({ name: "Alice" }); const u2 = new User({ name: "Alex", email: "alex@mail.com" }); console.log(u2); ``` It reads more clearly, especially with a large number of parameters. --- ### Option 3: Use **Static Factory Methods** This is a very popular approach in real projects: instead of multiple constructors, you create different "factories": ```javascript class User { constructor(name, age) { this.name = name; this.age = age; } static fromName(name) { return new User(name, null); } static fromObject(obj) { return new User(obj.name, obj.age); } } const u1 = User.fromName("Alice"); const u2 = User.fromObject({ name: "Alex", age: 30 }); console.log(u1, u2); ``` This way you can create "different constructors" under the hood, but they're actually different **static methods** that return an instance. --- ### Option 4: Use **Optional Parameters + Destructuring** ```javascript class Point { constructor(x = 0, y = 0) { this.x = x; this.y = y; } } const a = new Point(); // (0, 0) const b = new Point(10); // (10, 0) const c = new Point(5, 7); // (5, 7) ``` One constructor, but three ways to call it, all legal and convenient. --- ### Option 5: Inheritance (Different "Types" of Constructors) You can create different **subclasses**, each with its own constructor: ```javascript class User { constructor(name) { this.name = name; } } class Admin extends User { constructor(name, permissions) { super(name); this.permissions = permissions; } } const u = new User("Alice"); const a = new Admin("Alex", ["edit", "delete"]); ``` This is how "multiple constructors" are implemented **through a class hierarchy**. --- ## Summary | Approach | Is It Allowed | Example | | --- | --- | --- | | Multiple `constructor()` | No: syntax error | - | | A single constructor with argument checks | Yes | `if (typeof age === "undefined") ...` | | Parameter object | Yes | `constructor({ name, age }) {}` | | Factory methods | Yes | `User.fromName()` / `User.fromObject()` | | Inheritance | Yes | `class Admin extends User` | | Optional parameters | Yes | `constructor(x = 0, y = 0)` | --- **Conclusion:** > In JS a class can have only **one** `constructor()`, > but you can achieve the same flexibility using **checks, factories, or inheritance**.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.