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

As JavaScript applications grow, managing code and data can become complicated. To make programs easier to organize and reuse, developers often use a programming style called Object-Oriented Programming (OOP).

Object-Oriented Programming focuses on creating objects that contain both data and behavior.

Instead of writing everything in separate variables and functions, OOP helps you structure your code in a more organized way.

1. What Object-Oriented Programming (OOP) Means

Object-Oriented Programming is a way of writing code where you organize things into objects.

Each object represents something from the real world and contains:

  • Properties → data about the object

  • Methods → actions the object can perform

For example, think about a car.

A car has:

Properties

  • brand

  • color

  • speed

Methods

  • start()

  • drive()

  • stop()

In OOP, we create objects that behave in a similar way.

Example:

let car = {
  brand: "Toyota",
  color: "Red",
  start() {
    console.log("Car started");
  }
};

Here the object contains both information and behavior.

2. Real-World Analogy: Blueprint → Objects

To understand OOP better, imagine building cars in a factory.

Before making a car, engineers create a blueprint.

The blueprint describes:

  • how the car should look

  • what features it has

  • how it works

Using that blueprint, the factory can build many cars.

Example:

Blueprint → Car objects

  • Car 1 → Red Toyota

  • Car 2 → Blue Honda

  • Car 3 → Black BMW

In programming:

  • Blueprint → Class

  • Actual item → Object

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

3. What is a Class in JavaScript

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

It defines:

  • what properties an object will have

  • what methods it can use

Example:

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

Here:

  • Car is the class

  • It defines properties like brand and color

But the class itself does not represent a real car yet.

We must create objects from it.

4. Creating Objects Using Classes

To create an object from a class, JavaScript uses the new keyword.

Example:

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

Now we have two separate objects.

console.log(car1.brand); // Toyota
console.log(car2.color); // Blue

Both objects were created using the same class, which helps avoid repeating code.

This is one of the main benefits of OOP: code reusability.

5. Constructor Method

The constructor is a special method inside a class.

It runs automatically when a new object is created.

Its job is to initialize object properties.

Example:

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

let person1 = new Person("Aman", 22);

Here:

  • "Aman" becomes the value of name

  • 22 becomes the value of age

The constructor helps set up the object when it is first created.

6. Methods Inside a Class

Classes can also contain methods, which are functions that define the behavior of objects.

Example:

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

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

let user = new Person("Rahul");

user.greet();

Output:

Hello, my name is Rahul

Here:

  • greet() is a method

  • The object user can call that method

Methods allow objects to perform actions.

7. Basic Idea of Encapsulation

Encapsulation is the idea of keeping related data and functions together inside a class.

Instead of spreading data across many places, everything related to an object stays in one structure.

Example:

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

  showResult() {
    console.log(this.name + " scored " + this.marks);
  }
}

Here:

  • Student data (name, marks)

  • Student behavior (showResult())

are all inside the same class.

This makes the code:

  • easier to understand

  • easier to maintain

  • more organized

Why OOP is Useful

Object-Oriented Programming helps developers:

  • organize code better

  • reuse code using classes

  • represent real-world concepts in programs

  • make large applications easier to manage

Final Thoughts

Object-Oriented Programming is a powerful way to structure JavaScript programs.

To summarize:

  • OOP organizes code using objects

  • Classes act as blueprints

  • Objects are created from classes

  • Constructors initialize object data

  • Methods define object behavior

  • Encapsulation keeps related data and logic together

Once you understand these basics, you can build more structured and scalable JavaScript applications.

Understanding Object-Oriented Programming in JavaScript