JavaScript Constructors
Table of Contents + −
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.
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); // 30Let’s walk through what each line does:
constructor(name, age)defines the setup method and the two values it expects.this.name = namestores the incomingnameon the new object; herethisis that new object.this.age = agedoes the same for the age.new User("Alex", 30)builds a freshUserand passes"Alex"and30into the constructor.- The two
console.loglines 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.
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:
thispoints at the brand-new object thatnewjust created.this.name = namecreates anameproperty on that object and copies the parameter’s value into it.this.age = agecreates anageproperty 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.
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, sothisis thefirstobject and gets its ownnameandage.new User("Jordan", 25)runs the constructor again, sothisis a different object,second, with its own values.first.nameandsecond.nameread 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.
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); // 999console.log(mouse.title); // "Mouse"console.log(mouse.price); // 25Here is what happens step by step:
constructor(title, price)accepts the two details every product needs.this.title = titleandthis.price = pricestore those details on whichever objectthiscurrently points to.new Product("Laptop", 999)runs the constructor withthisaslaptop, giving it that title and price.new Product("Mouse", 25)runs it again withthisasmouse, giving it different values.- The
console.loglines 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:
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 = titleonly reassigns the local parameter, so nothing lands on the object and the title is lost once the constructor ends.this.price = priceusesthis, 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 writethis.title = title.
🔧 Try It Yourself!
- Write a
Bookclass with a constructor that takestitleandauthor. - Inside the constructor, assign both values using
this.. - Create two different books with
newand print their titles. - 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 usenew - ✅ It sets up the object’s initial properties from its parameters
- ✅
thisinside the constructor refers to the new instance being created - ✅ Every instance gets its own separate copy of the data
- ✅ Forgetting
this.or misspellingconstructorstops 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.