Course contentsShow
← JavaScript
Lesson 758 of 2,72318. ClassesFree lesson

Constructor Parameters

Passing arguments to constructors to initialize instance properties with specific values.

Constructor Parameters

What you'll learn: How to pass arguments into a constructor to initialize each instance with specific, custom values.

Passing Data to Your Constructor

You've already seen the constructor method inside a class. Now it's time to make it flexible. Just like any function, constructors can accept parameters—variables that let you pass in values when you create an object with new.

Instead of hardcoding the same values for every instance, you pass unique data as arguments:

class Person {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }
}

const alice = new Person('Alice', 30);
const bob = new Person('Bob', 25);

console.log(alice.name); // 'Alice'
console.log(bob.age);    // 25

When you call new Person('Alice', 30), JavaScript invokes the constructor with name = 'Alice' and age = 30, then assigns those values to the new object's properties.

Why This Matters

Without parameters, every object would start with the same property values—or you'd have to manually set them after creation. Parameters let you customize each instance at birth, making your classes reusable and practical.

Think of it like ordering a custom pizza: the constructor is the kitchen, and the parameters (size, toppings) specify exactly what you want. Every pizza follows the same recipe, but each one is uniquely yours.

You can use any number of parameters, provide default values, or even destructure objects—just like regular function parameters.

Key Takeaway: Constructor parameters let you pass unique values when creating instances, so each object can be initialized with its own custom data right from the start.