JavaScript Constructor — PBA Institute Tutorial
Chapter 12 · JavaScript Programming Series
12 min read Beginner

JavaScript Constructor

A constructor is a special method inside a class that runs automatically when a new object is created. Its main job is to set up initial values for the object's properties.

What is a Constructor?

A constructor is a special method named constructor() inside a class. It runs automatically whenever you create a new object with the new keyword and is mainly used to initialize properties.

Types of Constructors

🟢

Default Constructor

Empty constructor — created automatically when you don't define one.

📥

Parameterized

Accepts arguments to initialize each object differently.

🧰

With Default Values

Use default parameters: constructor(a=0).

🪜

With super()

Inside a child class, calls the parent constructor first.

🔒

With Validation

Check inputs inside the constructor and throw errors if invalid.

⚙️

Object Setup

Bundle setup logic — DOM elements, defaults, IDs.

Basic Features

  • A constructor is invoked automatically by new.
  • A class can have only one constructor.
  • Use this inside the constructor to set properties.
  • If you don't define one, JavaScript adds an empty default constructor.

Constructors in JavaScript

A constructor in JavaScript is a special method used to create and initialize objects inside a class. It runs automatically whenever a new object is created using the new keyword.

What is Constructor ?

Constructors are used to assign initial values to object properties and perform setup tasks for new objects created from a class.

Constructor Syntax

Basic Constructor Syntax
class Car{

  constructor(
  brand,
  year
  ){

    this.brand =
    brand;

    this.year =
    year;

  }

}

let c =

new Car(
"Toyota",
2024
);

Basic Constructor Example

Person Class
<script>

class Person{

  constructor(
  name,
  age
  ){

    this.name =
    name;

    this.age =
    age;

  }

}

let person1 =

new Person(
"John",
30
);

let person2 =

new Person(
"Alice",
25
);

document.write(
person1.name
);

document.write(
"<br>"
);

document.write(
person2.age
);

</script>
Output John
25

Your First Constructor Program

Student Constructor
<script>

class Student{

  constructor(
  name,
  roll
  ){

    this.name =
    name;

    this.roll =
    roll;

  }

  show(){

    document.write(

    this.name +

    " - Roll " +

    this.roll

    );

  }

}

let s =

new Student(
"Riya",
12
);

s.show();

</script>
Output Riya - Roll 12

Example 1 : Factorial Class

Factorial Using Constructor
<script>

var i;

class Factorial{

  constructor(){

    this.a = 0;

  }

  input(m){

    this.a = m;

    this.f = 1;

    for(i=1;i<=
    this.a;i++){

      this.f =
      this.f*i;

    }

  }

  display(){

    document.write(
    this.f
    );

  }

}

ob =

new Factorial();

ob.input(5);

ob.display();

</script>
Output 120

Example 2 : Default Constructor

Default Constructor
<script>

class Hello{

  constructor(){

    document.write(

    "Object Created!"

    );

  }

}

new Hello();

</script>
Output Object Created!

Example 3 : HCF & LCM

HCF and LCM Program
<script>

var i;

class HCFLCM{

  constructor(x,y){

    this.a = x;
    this.b = y;

  }

  calculate(){

    this.m =
    this.a*this.b;

    for(i=1;i<=
    this.m;i++){

      if(
      this.a%i==0
      &&
      this.b%i==0
      ){

        this.h = i;

      }

    }

    this.l =
    this.m/this.h;

    document.write(
    "HCF="
    +this.h
    );

    document.write(
    "<br>"
    );

    document.write(
    "LCM="
    +this.l
    );

  }

}

ob =

new HCFLCM(
10,
15
);

ob.calculate();

</script>
Output HCF = 5
LCM = 30

Example 4 : Default Parameters

Constructor With Defaults
<script>

class User{

  constructor(

  name="Guest",

  age=18

  ){

    this.name =
    name;

    this.age =
    age;

  }

  show(){

    document.write(

    this.name +

    " / " +

    this.age

    );

  }

}

new User().show();

document.write(
"<br>"
);

new User(
"Rokeiya",
20
).show();

</script>
Output Guest / 18
Rokeiya / 20

Example 5 : super()

Constructor With super()
<script>

class Animal{

  constructor(name){

    this.name =
    name;

  }

}

class Dog
extends Animal{

  constructor(
  name,
  breed
  ){

    super(name);

    this.breed =
    breed;

  }

}

let d =

new Dog(
"Rocky",
"Labrador"
);

document.write(

d.name +

" - " +

d.breed

);

</script>
Output Rocky - Labrador

Important Notes

  • Constructor name must be exactly constructor.
  • Only one constructor is allowed per class.
  • super() must be called before using this in child classes.
  • Constructors initialize object properties automatically.

Real-Life Use Cases

👤

User Registration

Create user objects from form data.

🛒

Shopping Cart

Initialize product details automatically.

🎮

Games

Create players and enemies with properties.

Frequently Asked Questions

Question Answer
Can we define multiple constructors? No, JavaScript allows only one constructor.
Is constructor mandatory? No, JavaScript adds a default constructor automatically.
What does super() do? Calls the parent class constructor.

Conclusion

Constructors are essential for initializing objects properly and building structured, reusable, and scalable JavaScript applications.

JavaScript All Chapters

Continue Learning

Previous

Go to Class Chapter

Next

Go to Inheritance Chapter