The Implicit Return with 'new'
You'll learn what new returns automatically and when that behavior changes.
The Default: Returning the New Object
When you use the new operator with a constructor function, something happens behind the scenes: new automatically returns the newly created object. You don't need to write return this explicitly—it just happens.
function Car(make) {
this.make = make;
// No return statement needed!
}
const myCar = new Car('Toyota');
console.log(myCar.make); // 'Toyota'
Even though Car doesn't have a return statement, myCar receives the new object that new created and populated.
The Exception: Explicit Object Returns
Here's the twist: if your constructor explicitly returns an object, new respects that choice and returns your object instead:
function Weird() {
this.value = 42;
return { different: true }; // Explicit object return
}
const result = new Weird();
console.log(result.value); // undefined
console.log(result.different); // true
The new operator says, "Oh, you're returning your own object? I'll use that instead."
Primitive Returns Are Ignored
If you return a primitive (number, string, boolean), new ignores it and returns the new object anyway:
function Ignore() {
this.name = 'Test';
return 42; // Primitive—ignored!
}
const obj = new Ignore();
console.log(obj.name); // 'Test'
Key Takeaway: The new operator implicitly returns the newly created object unless you explicitly return a different object—primitive returns are ignored.