codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🌐

JavaScript

30 / 65 topics
28JavaScript Objects29JavaScript Object Methods & this30JavaScript Constructor Functions31JavaScript Prototypes32JavaScript Classes33JavaScript Class Inheritance34JavaScript Getters and Setters
Tutorials/JavaScript/JavaScript Constructor Functions
🌐JavaScript

JavaScript Constructor Functions

Updated 2026-05-12
30 min read

JavaScript Constructor Functions

In object-oriented programming (OOP), one of the fundamental concepts is creating multiple instances of an object with similar properties and behaviors. In JavaScript, constructor functions provide a way to create objects using a blueprint or template. This tutorial will guide you through understanding how to use constructor functions along with the new keyword to instantiate multiple objects efficiently.

Introduction

Constructor functions in JavaScript are regular functions that are used to initialize new objects. When called with the new keyword, they perform several actions:

  1. A new empty object is created.
  2. The this keyword inside the function refers to this newly created object.
  3. The properties and methods defined within the constructor are added to this new object.
  4. The new object is returned from the function.

Constructor functions are typically named with an uppercase first letter to distinguish them from regular functions, although JavaScript does not enforce this convention.

Creating a Constructor Function

Let's start by creating a simple constructor function for a Car object:

script.js
1function Car(make, model, year) {
2this.make = make;
3this.model = model;
4this.year = year;
5}

In this example, the Car constructor function takes three parameters: make, model, and year. These parameters are used to set the properties of the new object.

Using the "new" Keyword

To create a new instance of a Car using the constructor function, you use the new keyword:

script.js
1const myCar = new Car('Toyota', 'Corolla', 2020);
2console.log(myCar.make); // Output: Toyota
3console.log(myCar.model); // Output: Corolla
4console.log(myCar.year); // Output: 2020
Output
Toyota
Corolla
2020

When you use the new keyword, JavaScript automatically sets up a new object and assigns it to this inside the constructor function. This allows you to add properties and methods to the object.

Adding Methods to Constructor Functions

You can also add methods to objects created by a constructor function. Here's an example of adding a method called displayInfo:

script.js
1function Car(make, model, year) {
2this.make = make;
3this.model = model;
4this.year = year;
5
6this.displayInfo = function() {
7 console.log(`${this.year} ${this.make} ${this.model}`);
8};
9}
10
11const myCar = new Car('Toyota', 'Corolla', 2020);
12myCar.displayInfo(); // Output: 2020 Toyota Corolla
Output
2020 Toyota Corolla

In this example, the displayInfo method is added directly to each instance of the Car. While this works, it's not efficient because each new object has its own copy of the method. This can lead to memory inefficiencies.

Using Prototypes

To avoid creating multiple copies of methods for each object, you can use prototypes. Prototypes are shared among all instances of a constructor function. We'll cover this in more detail in the next section, "JavaScript Prototypes."

Practical Example: A Library System

Let's create a practical example to demonstrate how constructor functions can be used to manage a simple library system.

script.js
1function Book(title, author) {
2this.title = title;
3this.author = author;
4
5this.displayInfo = function() {
6 console.log(`${this.title} by ${this.author}`);
7};
8}
9
10const book1 = new Book('1984', 'George Orwell');
11const book2 = new Book('To Kill a Mockingbird', 'Harper Lee');
12
13book1.displayInfo(); // Output: 1984 by George Orwell
14book2.displayInfo(); // Output: To Kill a Mockingbird by Harper Lee
Output
1984 by George Orwell
To Kill a Mockingbird by Harper Lee

In this example, the Book constructor function is used to create multiple book objects. Each book has its own title and author, but they share the same displayInfo method.

Summary

  • Constructor functions are used to create multiple instances of an object.
  • The new keyword is used to instantiate a new object from a constructor function.
  • Properties and methods can be added directly to objects created by a constructor function.
  • Adding methods directly to each instance can lead to memory inefficiencies; using prototypes is more efficient.

What's Next?

In the next tutorial, we'll explore JavaScript prototypes in detail. Prototypes allow you to share properties and methods among all instances of a constructor function, making your code more efficient and organized. Stay tuned!


PreviousJavaScript Object Methods & thisNext JavaScript Prototypes

Recommended Gear

JavaScript Object Methods & thisJavaScript Prototypes