Open In App

Object Oriented Programming in JavaScript

Last Updated : 07 Aug, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Object Oriented Programming (OOP) is a style of programming that uses classes and objects to model real-world things like data and behavior. A class is a blueprint that defines the properties and methods an object can have, while an object is a specific instance created from that class.

Why OOP is Needed:

Before OOP, when the code size grows and multiple people work on a project, there problems arise

  • Changing in one team's code causing other codes 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:

  • 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 .
OOPs-Concepts

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:

C++
// 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.

Read:

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.

Learn here: Abstraction in JavaScript

Encapsulation

Encapsulation is defined as wrapping up data and information under a single unit. In Object-Oriented Programming, encapsulation is defined as binding together the data and the functions that manipulate them together in a class.

Learn here: Encapsulation in JavaScript

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.

  • Sub Class: The class that inherits properties from another class is called Sub class or Derived Class.
  • Super Class: The class whose properties are inherited by a sub-class is called Base Class or Superclass.

Learn here: JavaScript Inheritance

Polymorphism

The word polymorphism means having many forms. In simple words, we can define polymorphism as the ability of an entity to behave different in different scenarios. person at the same time can have different characteristics.

Learn here: Polymorphism in JavaScript

These were all the essential OOP concepts in JavaScript, covering their complete mechanism and implementation. As OOP is an important part of JavaScript programming as it helps in building structured, reusable, and maintainable code. By using classes, objects, inheritance, polymorphism, encapsulation, and abstraction, developers can write cleaner programs that model real-world scenarios effectively while keeping the code easy to update and scale.


Introduction to Object Oriented Programming (OOPs)

Similar Reads