JavaScript Constructors

In the previous lesson, we learned how to define a class as a blueprint for objects. Now let’s look at the constructor, the special method that gives each new object its starting data.

🏗️ What is a Constructor?

A constructor is a special method named constructor() inside a class. It runs automatically the moment you create an object with the new keyword. Its job is to set up the object’s initial properties.

constructors.js
class User {
constructor(name, age) {
this.name = name;
this.age = age;
}
}
const user = new User("Alex", 30);
console.log(user.name); // "Alex"
console.log(user.age); // 30

Let’s walk through what each line does:

  • constructor(name, age) defines the setup method and the two values it expects.
  • this.name = name stores the incoming name on the new object; here this is that new object.
  • this.age = age does the same for the age.
  • new User("Alex", 30) builds a fresh User and passes "Alex" and 30 into the constructor.
  • The two console.log lines read those stored properties back off the object.

You never call the constructor yourself. Writing new User("Alex", 30) calls it for you and hands the arguments to the parameters.

🎯 The Role of this

Inside the constructor, this refers to the new object being created. When you write this.name = name, you attach a name property to that specific object and fill it with the value that was passed in.

constructors.js
class User {
constructor(name, age) {
this.name = name; // attach name to the new object
this.age = age; // attach age to the new object
}
}

Here is what each assignment is doing:

  • this points at the brand-new object that new just created.
  • this.name = name creates a name property on that object and copies the parameter’s value into it.
  • this.age = age creates an age property on the same object in the same way.

The parameter name is the value coming in, and this.name is the property being stored on the object. They can share a name, but they are different things.

this means the new instance

Every time you use new, JavaScript creates a fresh object and points this at it inside the constructor. That is how each object gets its own data.

👥 Each Instance Gets Its Own Data

Because the constructor runs once per object, every instance you create holds its own separate values. Creating two users from the same class gives you two independent objects.

constructors.js
class User {
constructor(name, age) {
this.name = name;
this.age = age;
}
}
const first = new User("Alex", 30);
const second = new User("Jordan", 25);
console.log(first.name); // "Alex"
console.log(second.name); // "Jordan"

Let’s trace how two separate objects come out of one class:

  • new User("Alex", 30) runs the constructor once, so this is the first object and gets its own name and age.
  • new User("Jordan", 25) runs the constructor again, so this is a different object, second, with its own values.
  • first.name and second.name read from two distinct objects, which is why they print different names.

Changing first does not affect second. The constructor built each one with its own name and age.

🛒 A Practical Example

Here is a Product class. Its constructor takes a title and a price, then stores them on each new product.

constructors.js
class Product {
constructor(title, price) {
this.title = title;
this.price = price;
}
}
const laptop = new Product("Laptop", 999);
const mouse = new Product("Mouse", 25);
console.log(laptop.title); // "Laptop"
console.log(laptop.price); // 999
console.log(mouse.title); // "Mouse"
console.log(mouse.price); // 25

Here is what happens step by step:

  • constructor(title, price) accepts the two details every product needs.
  • this.title = title and this.price = price store those details on whichever object this currently points to.
  • new Product("Laptop", 999) runs the constructor with this as laptop, giving it that title and price.
  • new Product("Mouse", 25) runs it again with this as mouse, giving it different values.
  • The console.log lines confirm each product kept its own title and price.

The same blueprint produced two products, each carrying its own details. This is the main reason constructors exist: to stamp out many objects that share a shape but hold different data.

⚠️ Common Mistakes to Avoid

Mistake Problem Solution
Misspelling the method name constructer does not run; properties never get set Spell it exactly constructor
Forgetting this. name = name sets a local variable, not a property Always write this.name = name
Not passing arguments to new The properties become undefined Pass values: new Product("Mouse", 25)

Here is the difference in code:

constructors.js
class Product {
constructor(title, price) {
title = title; // ❌ sets a local variable that disappears
this.price = price; // ✅ stores the value on the object
}
}

Let’s compare the two lines side by side:

  • title = title only reassigns the local parameter, so nothing lands on the object and the title is lost once the constructor ends.
  • this.price = price uses this, so the value is saved as a real property on the new product and stays around.
  • The fix for the broken line is to add this. and write this.title = title.

🔧 Try It Yourself!

  1. Write a Book class with a constructor that takes title and author.
  2. Inside the constructor, assign both values using this..
  3. Create two different books with new and print their titles.
  4. Try creating a book without passing arguments, then log its properties to see undefined.

🧩 What You’ve Learned

  • ✅ The constructor() method runs automatically when you use new
  • ✅ It sets up the object’s initial properties from its parameters
  • this inside the constructor refers to the new instance being created
  • ✅ Every instance gets its own separate copy of the data
  • ✅ Forgetting this. or misspelling constructor stops it from working

🚀 What’s Next?

Now that each object can set up its own data, we will learn how one class can build on another. Let’s continue to Inheritance.

Share & Connect