JavaScript Constructor function

Introduction

A constructor function allows you to create multiple objects with the same structure using a template. It is one of the foundational concepts of object-oriented programming (OOP) in JavaScript.

In this guide, you’ll learn everything about JavaScript constructor functions, including syntax, usage, examples, real-life applications, common mistakes, and interview questions.

What is a Constructor Function?

A constructor function is a special type of function used to create and initialize objects.

📖
Important Points:
  • First character of the constructor function should be in Capital letters.
  • Constructor function is used to create the Object through new keyword.

Syntax of Constructor Function


function ConstructorName(parameters) {
   this.property = value;
}

Basic Example


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

const user1 = new Person("John", 35);
const user2 = new Person("Tom", 30);
console.log(user1.name);
console.log(user2.name);

Output:

John
Tom

How new Keyword Works

When you use new, JavaScript performs the following steps:

  1. Creates a new empty object
  2. Sets this to the new object
  3. Links the object to the constructor’s prototype
  4. Returns the object

Example:


function Car(brand) {
   this.brand = brand;
}

const car1 = new Car("Toyota");
console.log(car1.brand);

Output:

Toyota

Adding Method in Constructor function

Now you want to add method fullName() in the constructor function.

Example:


function Employee(firstName, lastName){
     
this.firstName = firstName;
this.lastName = lastName;

this.fullName= function(){
return this.firstName+" "+this.lastName;
}

}

const emp1 = new Employee("John", "Taylor");
console.log(emp1.firstName);
console.log(emp1.lastName);

Output:

John
Taylor

Real-Life Examples: Create User Object


function User(username, email) {
   this.username = username;
   this.email = email;
}

const user = new User("john123", "john@test.com");
console.log(user.username);
console.log(user.email);

Output:

john123
john@test.com

Constructor vs Object Literal

Feature Constructor Function Object Literal
Reusability High Low
Multiple Objects Yes No
Structure Template Fixed

Common Mistakes

I will show you common mistakes, which mostly happens by Beginner.

1. Forgetting new Keyword


function Person(name) {
   this.name = name;
}

const p = Person("John"); 

Note: this will refer to global object.

2. Not Using Capital Letter


function person() {} // Not recommended

Constructor Function and Inheritance


function Animal(name) {
   this.name = name;
}

Animal.prototype.speak = function() {
   return this.name + " makes a sound";
};

function Dog(name) {
   Animal.call(this, name);
}

Dog.prototype = Object.create(Animal.prototype);

const dog = new Dog("Tomy");
console.log(dog.speak());

Output:

Tomy makes a sound

Javascript Constructor function – Interview Questions

Q 1: What is constructor function?
Ans: Used to create objects.
Q 2: How to call the constructor?
Ans: Using new.
Q 3: Naming convention?
Ans: Capital letter.
Q 4: What does this refer to?
Ans: Newly created object.
Q 5: Can constructors return a value?
Ans: Objects only.

Javascript Constructor function – Objective Questions (MCQs)

Q1. Constructor functions are used to ______.






Q2. Which keyword is used to call a constructor function?






Q3. Constructor function names usually start with a ______.






Q4. Inside a constructor, this refers to ______.






Q5. Methods shared among objects are added to the ______.






Conclusion

JavaScript constructor functions are a powerful way to create reusable object templates. They form the foundation of object-oriented programming in JavaScript and are essential for understanding how modern JavaScript classes work.

By mastering constructor functions, prototypes, and this, you can write more structured, efficient, and scalable JavaScript code.