Classes Cannot Be Called Without new
You'll learn: Why JavaScript classes must always be called with new, and what happens if you forget it.
The Rule
In JavaScript, classes cannot be invoked like regular functions. Unlike old-style constructor functions (which technically worked without new, even if incorrectly), classes enforce the use of the new keyword. If you try to call a class without it, JavaScript immediately throws a TypeError.
class Person {
constructor(name) {
this.name = name;
}
}
// Correct usage:
const alice = new Person('Alice'); // ✓ Works
// Incorrect usage:
const bob = Person('Bob'); // ✗ TypeError: Class constructor Person cannot be invoked without 'new'
Why This Matters
This is a safety feature. With old constructor functions, forgetting new could cause subtle bugs (like accidentally creating global variables). Classes eliminate that confusion entirely—they require new, making your intent explicit and preventing accidental misuse.
// Old constructor function (doesn't enforce 'new'):
function Animal(type) {
this.type = type; // Without 'new', 'this' could be the global object!
}
// Class (enforces 'new'):
class Plant {
constructor(species) {
this.species = species; // Cannot run without 'new'—throws error immediately
}
}
The Error You'll See
TypeError: Class constructor [ClassName] cannot be invoked without 'new'
This clear error message helps you catch the mistake immediately during development.
Key Takeaway: Classes must be called with new—calling them like regular functions throws a TypeError, which is a deliberate safeguard against common mistakes with constructor functions.