Suggest an editImprove this articleRefine the answer for “What is a class constructor?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)A **constructor** is a special class method in JavaScript that is called automatically when a new object is created via `new`. It is used to initialize the object: assigning initial values to properties, calling methods, and so on. **Key point:** if no constructor is defined, JavaScript adds an empty default constructor, and in derived classes `super()` must be called before `this` can be used.Shown above the full answer for quick recall.Answer (EN)ImageA constructor is a **special class method** in JavaScript that is called **automatically when a new object is created** via `new`. It is used for **initializing** the object: assigning initial values to properties, calling methods, and so on. Let's look at this in detail: --- ## What a Constructor Is ### Example: ```javascript class User { constructor(name, age) { this.name = name; // create an object property this.age = age; } sayHi() { console.log(`Hi, my name is ${this.name}, I am ${this.age}`); } } const user = new User('Oleh', 25); user.sayHi(); // Hi, my name is Oleh, I am 25 ``` > The `constructor()` method fires **at the moment** `new User(...)` **is called**, > and it is used to set the object's initial state. --- ## How the Constructor Works Under the Hood When you write: ```javascript const user = new User('Oleh', 25); ``` the following happens: 1. A **new empty object** `{}` is created. 2. This object is linked to the `User.prototype` prototype. 3. `constructor('Oleh', 25)` is called inside the class. 4. Code runs inside the constructor, usually `this.name = ...`. 5. The created object (`this`) is returned. > In other words, `new` creates the object itself and returns it, you don't need to write `return`. --- ## Important Features ### 1. The constructor is called **automatically** with `new` ```javascript new User('Alex', 30); // calls constructor() ``` --- ### 2. A class can have **only one constructor** ```javascript class Test { constructor() {} constructor() {} // Error: Duplicate constructor } ``` > If you need different behavior, you can use default parameters or conditions inside the constructor. --- ### 3. The constructor can be **omitted** If you don't define a `constructor`, one is created **by default**: ```javascript class User { sayHi() { console.log('Hi!'); } } const user = new User(); user.sayHi(); // Hi! ``` Under the hood, the browser adds this itself: ```javascript constructor() {} ``` --- ### 4. The constructor in **derived classes** When a class extends another one via `extends`, it must **call the parent's constructor** with `super()` before using `this`. ```javascript class Person { constructor(name) { this.name = name; } } class Student extends Person { constructor(name, group) { super(name); // call the parent's constructor this.group = group; } } const s = new Student('Oleh', 'Frontend-2025'); console.log(s.name); // Oleh console.log(s.group); // Frontend-2025 ``` > If `super()` is not called, you get an error: > `ReferenceError: Must call super constructor in derived class before accessing 'this'`. --- ### 5. The constructor can return **a different object** Normally a constructor has no `return`, but if it explicitly returns an **object**, that object replaces the created instance: ```javascript class Example { constructor() { return { custom: true }; } } const obj = new Example(); console.log(obj); // { custom: true } ``` > If it returns **something other than an object** (for example, a number or a string), that return value is ignored. --- ## Example: A Class With Logic in the Constructor ```javascript class Product { constructor(name, price) { this.name = name; this.price = price > 0 ? price : 0; // validation } getInfo() { return `${this.name}: $${this.price}`; } } const p1 = new Product('Shirt', 2500); const p2 = new Product('Coat', -100); console.log(p1.getInfo()); // Shirt: $2500 console.log(p2.getInfo()); // Coat: $0 ``` > A constructor is often used for validation and data preparation. --- ## Quick Comparison: `constructor` vs Regular Methods | Feature | `constructor()` | Regular Methods | | --- | --- | --- | | Called automatically with `new` | Yes | No | | Can only have one | Yes | No | | Used for object initialization | Yes | Sometimes | | Can return a different object | Yes | No | | Accessible via `this` | Yes | Yes | --- ## Summary > A **class constructor** is a special method > that is called when an object is created via `new`, > and it is used for **initializing properties** and **preparing the instance**. Formula to remember: ```javascript class X { constructor(...) { this.properties = ... } } const obj = new X(...); ```For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.