JavaScript new: Constructor Calls and Return Values
Quick answer
new invokes a constructible value as a constructor. For ordinary base constructors, it creates a receiver linked to the constructor prototype and returns that receiver unless an object is explicitly returned.
How it works
An ordinary constructor assigns instance state through this. If its public prototype is an object, the new receiver uses it as its prototype. A returned object replaces the receiver; a returned primitive is ignored for ordinary base constructors. new.target identifies the constructor targeted by construction, and is undefined in ordinary calls. Arrow functions cannot be constructed.
function Item(n) { this.n = n; return 99; }const x = new Item(4); console.log(x.n,Object.getPrototypeOf(x)===Item.prototype);Expected output in Node.js:
4 trueThe primitive return is ignored and the new receiver survives.
function Item() { this.n = 1; return {n:8}; }const x = new Item(); console.log(x.n,x instanceof Item);Expected output in Node.js:
8 falseThe explicitly returned object replaces the default receiver.
Common mistakes
Not every callable is constructible. Use new only with constructors.
Practice
Compare a constructor that returns an object with one that returns a primitive.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
JavaScript new: Constructor Calls and Return Values — Knowledge Check
Apply the taught concept and predict the output.
What does an explicit object return do in an ordinary base constructor?
Which callable is not constructible?
What is new.target in an ordinary function call without new?
What is the exact console output in JavaScript new: Constructor Calls and Return Values example 1?
function Item(n) { this.n = n; return 99; }const x = new Item(4); console.log(x.n,Object.getPrototypeOf(x)===Item.prototype);What is the exact console output in JavaScript new: Constructor Calls and Return Values example 2?
function Item() { this.n = 1; return {n:8}; }const x = new Item(); console.log(x.n,x instanceof Item);