What is the purpose of the `new` keyword?
Quiz · JavaScript
Easy
5 min
oop
this
prototypes
TL;DR
new F(...args) creates a new object whose prototype is F.prototype, runs F with this bound to that object, and returns the object, unless F explicitly returns a different object. It's how constructor functions and classes produce instances that share methods through their prototype.
The four steps
function User(name) {
this.name = name;
}
User.prototype.greet = function () { return `Hi, ${this.name}`; };
const u = new User('Ada');
- Create a fresh object:
{}. - Set its
[[Prototype]]toUser.prototype. - Call
Userwiththis= that object and the given arguments. - If
Userreturns an object (including arrays/functions), use it; otherwise return the new object.
A rough equivalent:
function myNew(F, ...args) {
const obj = Object.create(F.prototype);
const result = F.apply(obj, args);
return result !== null && (typeof result === 'object' || typeof result === 'function') ? result : obj;
}
(Real new with classes differs: derived classes create this in the base constructor via super(), and class constructors cannot be called via apply.)
Return-value rules
function A() { this.x = 1; return { y: 2 }; }
new A(); // { y: 2 }: returned object wins
function B() { this.x = 1; return 42; }
new B(); // { x: 1 }: primitive return values are ignored
What happens without new
function Point(x) { this.x = x; }
const p = Point(1);
// sloppy mode: this === globalThis → creates a global `x`, p is undefined
// strict mode: this === undefined → TypeError
Classes protect you: class P {}; P() throws TypeError: Class constructor P cannot be invoked without 'new'. In plain functions you can detect it with new.target:
function Point(x) {
if (!new.target) return new Point(x);
this.x = x;
}
new.target is also how abstract base classes refuse direct instantiation:
class Shape {
constructor() {
if (new.target === Shape) throw new TypeError('Shape is abstract');
}
}
What can and can't be constructed
- ✅ Regular
functiondeclarations/expressions, classes, most built-ins (new Map(),new Date(),new Promise()). - ❌ Arrow functions, methods defined with shorthand syntax,
asyncfunctions and generators throwTypeError: … is not a constructor. - ⚠️
SymbolandBigIntmust not be called withnew.String,NumberandBooleancan be, but create wrapper objects (new Boolean(false)is truthy), so call them withoutnewfor conversion. Date()withoutnewreturns a string, whilenew Date()returns aDateobject. Some built-ins behave differently with and withoutnew.
Pitfalls
- Forgetting
newwith constructor functions (mitigated by classes, linting andnew.targetguards). - Returning an object from a constructor by accident discards the instance and its prototype methods.
- Using
newfor factory functions that already return objects works but wastes an allocation and confuses readers.
In an interview, mention…
- The four steps, especially the prototype link (which is what makes shared methods work).
- Return-value override rules and
new.target. - Which callables are constructible, and why classes are safer than constructor functions.
References
Spotted a mistake or a rights concern? Report an issue