100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
JavaScript

Constructors in JavaScript

How constructor functions and class constructors initialize new objects, run automatically with `new`, and support default parameters.

OOP in JavaScriptIntermediate8 min readJul 8, 2026
Analogies

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

javascript
// 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

javascript
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

text
Constructing: The Hobbit
The Hobbit J.R.R. Tolkien
Constructing: Untitled Draft
Untitled Draft Unknown
legacy
true

6. Key Takeaways

  • The constructor() method runs automatically each time new ClassName(...) is invoked.
  • A class may define only one constructor(); a second one is a SyntaxError.
  • Constructors support default parameter values, evaluated left to right when arguments are missing.
  • Class constructors must be called with new; calling them directly throws a TypeError.
  • Pre-ES6 constructor functions behave similarly but lack that new-only safety guarantee.

Practice what you learned

Was this page helpful?

Topics covered

#JavaScript#JavaScriptProgrammingStudyNotes#Programming#ConstructorsInJavaScript#Constructors#Syntax#Explanation#Example#StudyNotes#SkillVeris