Skip to main content

Command Palette

Search for a command to run...

Understanding Object-Oriented Programming in JavaScript

Updated
4 min read
Understanding Object-Oriented Programming in JavaScript

Hey Everyone,

In this blog, we will learn about Object-Oriented Programming in JavaScript.

As applications grow larger, managing code becomes more difficult.
To organize code better and make it reusable, developers often use a programming style called Object-Oriented Programming (OOP).

What is Object-Oriented Programming?

Object-Oriented Programming (OOP) is a way of writing code where we organize data and behavior into objects.

An object contains:

  • Properties → data

  • Methods → actions

Example idea:

A car object might have:

Properties:

  • brand

  • color

  • speed

Methods:

  • start()

  • stop()

  • accelerate()

OOP helps us structure code in a way that mirrors real-world entities.


Real-World Analogy: Blueprint → Objects

Imagine you are building cars.

First, engineers create a blueprint for a car.
From that blueprint, many cars can be produced.

Blueprint (Car Design)
        ↓
Car Object 1
Car Object 2
Car Object 3

In programming:

  • Blueprint → Class

  • Car → Object (Instance)

A class defines the structure, and objects are created from that class.


What is a Class in JavaScript?

A class is like a template used to create objects.

Example:

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

Here:

  • Car is the class

  • brand and color are properties

The class describes what every car object should look like.


Creating Objects Using Classes

Once a class is defined, we can create objects using the new keyword.

Example:

let car1 = new Car("Toyota", "Red");
let car2 = new Car("Honda", "Blue");

Now we have two objects created from the same class.

Car Class
   ↓
car1 → Toyota, Red
car2 → Honda, Blue

Each object has its own values but follows the same structure.


The Constructor Method

The constructor is a special method used to initialize object properties.

It runs automatically when a new object is created.

Example:

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

Creating objects:

let person1 = new Person("Rahul", 25);
let person2 = new Person("Anita", 30);

The constructor assigns values to each object.


Methods Inside a Class

Classes can also contain methods, which define behaviors.

Example:

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

  greet() {
    console.log("Hello, my name is " + this.name);
  }
}

Using the method:

let person1 = new Person("Rahul", 25);

person1.greet();

Output:

Hello, my name is Rahul

The method uses this to access object properties.


Basic Idea of Encapsulation

Encapsulation means keeping data and behavior together inside an object.

For example:

class BankAccount {
  constructor(owner, balance) {
    this.owner = owner;
    this.balance = balance;
  }

  deposit(amount) {
    this.balance += amount;
  }
}

Here:

  • The data (owner, balance)

  • The behavior (deposit)

are packaged together inside one class.

This helps keep code organized and easier to maintain.


Example: Student Class

Let’s create a simple Student class.

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

  printDetails() {
    console.log(this.name + " is " + this.age + " years old.");
  }
}

Creating objects:

let student1 = new Student("Aman", 20);
let student2 = new Student("Riya", 21);

student1.printDetails();
student2.printDetails();

Output:

Aman is 20 years old.
Riya is 21 years old.

This shows how one class can create multiple objects.


Why OOP is Useful

Object-Oriented Programming helps us:

  • Organize code better

  • Reuse code using classes

  • Model real-world systems easily

  • Keep data and behavior together

This makes programs easier to scale and maintain.


Practice Assignment

Try this yourself.

1. Create a Class

Create a class called Student.

2. Add Properties

Add properties:

  • name

  • age

3. Add a Method

Create a method that prints student details.

Example output:

Aman is 20 years old

4. Create Multiple Objects

let s1 = new Student("Aman", 20);
let s2 = new Student("Riya", 22);
let s3 = new Student("Kabir", 19);

Call the method for each object.


And now, you know what is Object-Oriented Programming in JavaScript.

If you have any doubt or want to connect, feel free to drop a comment — I’d be happy to help.

Thanks for reading, and see you in the next blog!

Peace ✌️ and Happy Learning!