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.
function User(name) {
this.name = name;
}
User.prototype.greet = function () { return `Hi, ${this.name}`; };
const u = new User('Ada');
{}.[[Prototype]] to User.prototype.User with this = that object and the given arguments.User returns 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.)
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
newfunction 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');
}
}
function declarations/expressions, classes, most built-ins (new Map(), new Date(), new Promise()).async functions and generators throw TypeError: … is not a constructor.Symbol and BigInt must not be called with new. String, Number and Boolean can be, but create wrapper objects (new Boolean(false) is truthy), so call them without new for conversion.Date() without new returns a string, while new Date() returns a Date object. Some built-ins behave differently with and without new.new with constructor functions (mitigated by classes, linting and new.target guards).new for factory functions that already return objects works but wastes an allocation and confuses readers.new.target.Spotted a mistake or a rights concern? Report an issue