Skip to main content

What does return do inside a constructor?

1. Default Constructor Behavior

When you create an instance of a class:

javascript
class User { constructor(name) { this.name = name; } } const u = new User("Alice");

Under the hood, JS does roughly the following:

  1. Creates a new empty object {}.
  2. Sets that object's prototype → User.prototype.
  3. Calls constructor with this referring to that new object.
  4. Returns this from 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

javascript
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.

javascript
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):

javascript
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); // true

Here return this isn't needed, because JS does that on its own, but return Database.instance substitutes 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:

javascript
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?

javascript
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 constructor

In subclasses, super() is required, even if you return your own object.


6. All the Rules in One Place

ScenarioWhat Is Returned
No returnthis (the created instance)
return thisthe same instance
return { ... }this object is substituted in place of this
return 42 / "hi" / trueignored, this is returned
Subclass without super()Error (must call super() before return)

Example to Understand It

javascript
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

  • return in the constructor is not required, by default this is 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 before return.
  • This behavior is useful for patterns like Singleton, Factory, Proxy, and so on.

Short Answer

Interview ready
Premium

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