What does return do inside a constructor?
1. Default Constructor Behavior
When you create an instance of a class:
class User {
constructor(name) {
this.name = name;
}
}
const u = new User("Alice");Under the hood, JS does roughly the following:
- Creates a new empty object
{}. - Sets that object's prototype →
User.prototype. - Calls
constructorwiththisreferring to that new object. - Returns
thisfrom the constructor (by default).
In other words, if you return nothing, return this happens implicitly.
2. What Happens if You Explicitly Use return?
If return Returns an Object, It Replaces this
class User {
constructor(name) {
this.name = name;
return { custom: "a different object" };
}
}
const u = new User("Alice");
console.log(u); // { custom: "a different object" }In other words, if you return an object, it replaces the instance being created.
If return Returns a Primitive (Number, String, Boolean, etc.)
Primitives are ignored, and the original this is returned.
class User {
constructor(name) {
this.name = name;
return 42;
}
}
const u = new User("Alice");
console.log(u); // User { name: "Alice" }Important: the constructor cannot "overwrite" this with a primitive.
3. Why It Works This Way
JS lets you return an object from a constructor so that you can substitute the instance when you need to return something special.
Example: implementing a singleton (one object for every call):
class Database {
constructor() {
if (Database.instance) {
return Database.instance; // always returns the same object
}
this.connected = true;
Database.instance = this;
}
}
const a = new Database();
const b = new Database();
console.log(a === b); // trueHere
return thisisn't needed, because JS does that on its own, butreturn Database.instancesubstitutes the instance being created.
4. Can You Use return in Classes Without extends?
Yes, you can, the behavior is the same.
But if a class inherits (extends) from another and you return an object,
it also replaces this:
class A {}
class B extends A {
constructor() {
super();
return { name: "replacement" };
}
}
const b = new B();
console.log(b); // { name: "replacement" }5. What Happens if You Don't Call super() in a Subclass and Try to return?
class A {}
class B extends A {
constructor() {
// Error, even though there is a return
return { test: 1 };
}
}
new B(); // ReferenceError: Must call super constructor before returning from derived constructorIn subclasses, super() is required, even if you return your own object.
6. All the Rules in One Place
| Scenario | What Is Returned |
|---|---|
No return | this (the created instance) |
return this | the same instance |
return { ... } | this object is substituted in place of this |
return 42 / "hi" / true | ignored, this is returned |
Subclass without super() | Error (must call super() before return) |
Example to Understand It
class Example {
constructor(x) {
this.x = x;
if (x > 10) return { value: "too large" };
}
}
console.log(new Example(5)); // Example { x: 5 }
console.log(new Example(20)); // { value: "too large" }If
x > 10, its "own" object is returned, otherwise, a regular instance of the class.
Summary
returnin the constructor is not required, by defaultthisis returned.- If you return an object, it replaces
this. - If you return a primitive, it is ignored.
- In classes that inherit from another,
super()is required beforereturn. - This behavior is useful for patterns like Singleton, Factory, Proxy, and so on.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.