Skip to main content

Command Palette

Search for a command to run...

Understanding Object-Oriented Programming (OOP) in JavaScript

Updated
4 min read

When building larger applications in JavaScript, organizing code becomes very important. This is where Object-Oriented Programming (OOP) helps.

OOP is a programming style that allows developers to structure code using objects and classes, making it easier to manage, reuse, and maintain.

In this article, we will understand the basics of OOP in JavaScript using simple examples.


What is Object-Oriented Programming (OOP)?

Object-Oriented Programming (OOP) is a programming concept where we organize code using objects that contain data and functions together.

In simple terms:

  • Objects represent real-world entities

  • Classes act like blueprints for creating those objects

This helps developers write clean, reusable, and scalable code.


Real-World Analogy: Blueprint → Objects

To understand OOP easily, think about building cars.

A blueprint is used to design a car.

From that single blueprint, we can create many cars.

Example:

Blueprint → Car design
Objects → Actual cars produced from that design

Example cars created from the same blueprint:

  • Car 1 – Red Toyota

  • Car 2 – Blue Honda

  • Car 3 – Black BMW

Even though the cars are different, they all follow the same blueprint structure.

In programming:

  • Class = Blueprint

  • Object = Instance created from that blueprint


What is a Class in JavaScript?

A class in JavaScript is a template used to create objects.

It defines:

  • properties (data)

  • methods (functions)

Example:

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

Here:

  • Car is a class

  • brand and color are properties


Creating Objects Using Classes

Once a class is created, we can create objects from it using the new keyword.

Example:

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

Now we have two different objects:

car1 → Toyota, Red
car2 → Honda, Blue

Both were created using the same class blueprint.


The Constructor Method

The constructor is a special method inside a class.

It runs automatically when an object is created.

It is used to initialize object properties.

Example:

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

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

console.log(person1.name);

Output:

Rahul

Here:

  • constructor() sets the values for name and age when the object is created.

Methods Inside a Class

Methods are functions defined inside a class.

They describe what the object can do.

Example:

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

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

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

person1.greet();

Output:

Hello, my name is Rahul

Here:

  • greet() is a method inside the class.

Basic Idea of Encapsulation

Encapsulation means bundling data and methods together inside a class.

Instead of handling data separately, everything related to an object is kept in one place.

Example:

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

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

Here:

  • owner and balance are data

  • deposit() is a method

Both are encapsulated inside the class.

This keeps code organized and secure.


Why OOP is Useful

Object-Oriented Programming helps in many ways:

1. Code Reusability

We can reuse the same class to create many objects.

2. Better Code Organization

Data and methods stay together.

3. Easier Maintenance

Large programs become easier to manage.

Object-Oriented Programming is a powerful concept that helps developers write clean and reusable code.

In this article we learned:

  • What Object-Oriented Programming is

  • Real-world analogy of blueprint and objects

  • What classes are in JavaScript

  • How to create objects using classes

  • Constructor method

  • Methods inside a class

  • Basic idea of encapsulation

Learning OOP is an important step toward building larger and more structured JavaScript applications.