Skip to main content

Command Palette

Search for a command to run...

Understanding Object-Oriented Programming in JavaScript

Updated
•3 min read•View as Markdown

Object-Oriented Programming (OOP) is a programming style that helps developers organize code in a clean and reusable way. Instead of writing everything as separate functions and variables, OOP groups related data and behavior together into objects.

In simple words, OOP allows us to represent real-world things inside our code.

What Does OOP Mean?

Object-Oriented Programming is a way of structuring code using objects. An object contains properties (data) and methods (functions that perform actions).

For example, a car in the real world has properties like color, brand, and speed. It also has actions like start, stop, and accelerate. In programming, we can represent this using an object.

Real-World Analogy: Blueprint and Objects

Think about building a house or manufacturing cars.

Before creating multiple cars, engineers first design a blueprint. This blueprint defines what every car will contain.

In JavaScript, this blueprint is called a class.

  • Class → Blueprint

  • Object → Actual item created from the blueprint

For example:

  • Class: Car

  • Objects: BMW, Audi, Tesla cars created from that class.

What is a Class in JavaScript?

A class is a template used to create objects. It defines the properties and methods that objects created from it will have.

Example:

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

  start() {
    console.log(this.brand + " car started");
  }
}

Here:

  • Car is the class.

  • constructor is used to initialize values.

  • start() is a method.

Creating Objects from a Class

Once the class is created, we can create multiple objects from it.

const car1 = new Car("Toyota", "Red");
const car2 = new Car("BMW", "Black");

car1.start();
car2.start();

Each object has its own data but uses the same structure defined by the class.

Understanding the Constructor Method

The constructor is a special method inside a class. It runs automatically when a new object is created.

It is mainly used to set the initial values of properties.

Example:

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

Here, this.brand and this.color belong to the object that is created.

Methods Inside a Class

Methods are functions that belong to a class. They define actions that objects can perform.

Example:

start() {
  console.log("Car started");
}

All objects created from the class can use this method.

Basic Idea of Encapsulation

Encapsulation means keeping related data and functions together inside a class.

This helps in organizing code better and protecting data from unnecessary changes.

Instead of writing separate variables and functions everywhere, OOP keeps them inside a structured object.

This makes the code:

  • Easier to understand

  • Easier to maintain

  • Reusable in large projects

Why OOP is Useful

Object-Oriented Programming provides many benefits:

  • Code reusability – one class can create many objects

  • Better organization of code

  • Easier maintenance in large applications

  • Real-world representation in programming

Because of these advantages, OOP is widely used in modern JavaScript development.

Assignment

Try creating your own class.

Create a class called Student.

Requirements:

  1. Add properties: name and age.

  2. Create a method called printDetails().

  3. The method should print the student's name and age.

  4. Create multiple student objects and call the method.

Example idea:

const student1 = new Student("Rahul", 20);
const student2 = new Student("Anita", 22);