Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is constructor in JavaScript

Understanding Constructors

Imagine you're an architect, designing houses. You'll want to create a blueprint that you can use over and over again to build new houses, without having to reinvent the wheel each time. In the world of JavaScript, a constructor is like that blueprint. It's a special method that helps in creating and initializing an object created from a class. A class, in simple terms, is a blueprint for creating objects (a particular data structure).

Making Sense of Constructors

In JavaScript, constructors play a vital role in object-oriented programming (OOP). OOP is a style of coding that's organized around objects rather than actions and data rather than logic. A constructor is a function that initializes an object. Think of it as a 'builder' of the object.

Here's a simple example:

class Car {
  constructor(brand, model, year) {
    this.brand = brand;
    this.model = model;
    this.year = year;
  }
}

const myCar = new Car('Toyota', 'Corolla', 2005);

In this code, Car is a class and constructor is a method in the class. When we create a new Car object, we call the constructor method and pass in the arguments for brand, model, and year. The new keyword triggers the constructor method.

Getting Deeper Into the Constructor Method

The constructor method is special because it gets called whenever we create a new object in our class. This is extremely useful because it means we can set up our object with all the data it needs right from the start.

Here's another example:

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

  bark() {
    return `${this.name} says woof!`;
  }
}

const myDog = new Dog('Rex', 'Labrador', 3);
console.log(myDog.bark()); // Rex says woof!

In this Dog class, the constructor method is creating and initializing three properties: name, breed, and age. We also have an additional method bark. When we create a new Dog object, we're able to use all of these properties and methods.

What if We Don’t Define a Constructor?

If you don't define a constructor method in your class, JavaScript will add an invisible and empty constructor method.

class Bike {
}

// is the same as

class Bike {
  constructor() { }
}

In these cases, when you create a new object, the empty constructor method is called, but since it's empty, it doesn't do anything.

Conclusion

The beauty of JavaScript shines brightly when you start exploring constructors. They allow us to encapsulate the creation and initialization of objects in a neat and organized manner, reducing chances of code repetition and enhancing readability. Just like an architect using a blueprint to build houses, you can now use constructors to create and manage your objects. With this newfound knowledge, go ahead and start building! Remember, every line of code you write is a brick in your digital construction. Happy coding!