1. Introduction
A constructor is a special method (or function) responsible for creating and initializing an object. In JavaScript, constructors can take the form of a constructor() method inside a class, or a regular function invoked with the new keyword (the pre-ES6 pattern). Either way, the constructor's job is to set up the initial state — assigning properties to this — for every new instance.
Cricket analogy: A constructor is like the process of kitting out a new player for their debut — assigning their squad number, position, and gear to that specific player the moment they join the team, setting their initial state before the first ball is bowled.
2. Syntax
// Class-based constructor
class Person {
constructor(name, age = 18) {
this.name = name;
this.age = age;
}
}
const p = new Person("Sam");
// Pre-ES6 constructor function
function PersonFn(name) {
this.name = name;
}
const q = new PersonFn("Rae");3. Explanation
The constructor runs automatically whenever new ClassName(...) is called. Under the hood, new creates a fresh empty object, links its prototype to ClassName.prototype, executes the constructor body with this bound to that new object, and returns the object (unless the constructor explicitly returns a different object). A class can have at most one constructor() method; defining two throws a SyntaxError.
Cricket analogy: Calling new Player(...) is like the automatic process a franchise runs every time it signs a player: a blank profile is created, it's automatically linked to the team's standard training program (the prototype), the signing paperwork (constructor body) fills in that specific player's details, and the finished profile is handed back — and a franchise can only have one official signing procedure, not two conflicting ones.
Constructors support default parameters just like regular functions, letting you supply fallback values when an argument is omitted. Older "constructor function" style (a plain function called with new) still works and produces functionally equivalent objects, but lacks class-only features such as private fields and enforced new-only invocation.
Cricket analogy: A constructor with default parameters is like a franchise's signing form that auto-fills 'domestic player' if nationality is left blank; the old pre-academy scouting method of just handing a player a squad number still technically works, but it lacks modern academy features like confidential medical records or a rule that you must go through official trials.
Class constructors cannot be called without new. Invoking Person() instead of new Person() throws a TypeError: Class constructor Person cannot be invoked without 'new'. Regular constructor functions do NOT have this protection — calling PersonFn("x") without new silently sets name on the global object (or throws in strict mode), which is a common source of bugs in older code.
4. Example
class Book {
constructor(title, author = "Unknown") {
this.title = title;
this.author = author;
console.log(`Constructing: ${title}`);
}
}
const b1 = new Book("The Hobbit", "J.R.R. Tolkien");
console.log(b1.title, b1.author);
const b2 = new Book("Untitled Draft");
console.log(b2.title, b2.author);
function OldStyle(name) {
this.name = name;
}
const o = new OldStyle("legacy");
console.log(o.name);
try {
Book("No New Keyword");
} catch (e) {
console.log(e instanceof TypeError);
}5. Output
Constructing: The Hobbit
The Hobbit J.R.R. Tolkien
Constructing: Untitled Draft
Untitled Draft Unknown
legacy
true6. Key Takeaways
- The
constructor()method runs automatically each timenew ClassName(...)is invoked. - A class may define only one
constructor(); a second one is aSyntaxError. - Constructors support default parameter values, evaluated left to right when arguments are missing.
- Class constructors must be called with
new; calling them directly throws aTypeError. - Pre-ES6 constructor functions behave similarly but lack that
new-only safety guarantee.
Practice what you learned
1. What happens when a class constructor is invoked without the `new` keyword?
2. How many `constructor()` methods can a single class body define?
3. In `constructor(title, author = "Unknown")`, what is `author` when `new Book("X")` is called with one argument?
4. What key safety difference exists between a class constructor and a pre-ES6 constructor function?
5. What does the `new` operator do before running the constructor body?
Was this page helpful?
You May Also Like
Classes in JavaScript
How ES6 class syntax provides a cleaner, structured way to create objects and implement object-oriented patterns on top of JavaScript's prototype system.
Inheritance in JavaScript
How the `extends` and `super` keywords let one class build on another, sharing and overriding behavior while respecting strict initialization order.
Prototypes and Prototypal Inheritance in JavaScript
The mechanism underlying every JavaScript object: how property lookups walk the prototype chain, and how `prototype`, `__proto__`, and `Object.getPrototypeOf` relate.
Default and Rest Parameters in JavaScript
Use default parameter values and gather variable numbers of arguments with rest parameters.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics