Object-Oriented Programming (OOP) in JavaScript is a programming paradigm based on objects that contain data (properties) and behavior (methods). It helps organize code in a reusable and modular way.
- Uses objects and prototypes/classes to structure code logically.
- Supports key concepts like encapsulation, inheritance, and polymorphism.
- Improves code reusability, scalability, and maintainability.
Purpose of OOP
Before OOP, when the code size grows and multiple people work on a project, there are problems that arise.
- Changes in one team's code can cause other code to break. Hence difficult to maintain.
- Large number of parameters during function calls.
- Difficult to divide and maintain code across teams.
- Limited Code Reusability
- Not scalable as the code is not modular.
Object Oriented Programming (OOP) solves these problems by combining data and the functions that operate on it into a single unit called an object. This approach has following main features
- Encapsulation ensures that one team can change data representation and algorithms without causing other team's code change.
- Inheritance ensures code reuse.
- Polymorphism allows objects to behave differently using the same interface.

Objects
In JavaScript, an object is a collection of data (properties) and actions (methods) stored as key–value pairs.
- Properties hold values like strings, numbers, or even other objects.
- Methods are functions inside the object that define what it can do.
Objects let you group related data and functionality together in one place.
Classes
In JavaScript, a class is a blueprint for creating objects with specific properties and methods. A class itself doesn’t hold values, it describes what an object should have and do. You create actual objects from a class using the new keyword.
Example:
// Class definition
class Car {
constructor(brand, model) {
this.brand = brand; // property
this.model = model; // property
}
// method
showDetails() {
console.log(`This car is a ${this.brand} ${this.model}.`);
}
}
// Creating objects from the class
const car1 = new Car("Toyota", "Corolla");
const car2 = new Car("Honda", "Civic");
// Using the objects
car1.showDetails(); // This car is a Toyota Corolla.
car2.showDetails(); // This car is a Honda Civic.
Output
This car is a Toyota Corolla. This car is a Honda Civic.
Abstraction
Abstraction is one of the key features of object-oriented programming in JavaScript. It means showing only the essential information and hiding unnecessary details from the user. Data abstraction refers to exposing only what is necessary to interact with the object while keeping the background details or internal logic hidden.
Encapsulation
Encapsulation refers to the concept of combining data and the methods that operate on that data into a single unit. In Object-Oriented Programming, it means binding variables and functions together within a class.
Inheritance
The capability of a class to derive properties and characteristics from another class is called Inheritance. Inheritance is one of the most important features of Object-Oriented Programming.
- Subclass: The class that inherits properties from another class is called subclass or derived class.
- Superclass: The class whose properties are inherited by a subclass is called Base Class or superclass.
Polymorphism
Polymorphism in JavaScript is an OOP concept where an object or method can take multiple forms and behave differently in different situations. It helps in writing flexible and reusable code.
- Polymorphism allows the same method to perform different actions based on the object.
- It improves code reusability, flexibility, and maintainability.
- OOP concepts like classes, inheritance, encapsulation, abstraction, and polymorphism help build structured and scalable applications.