Understanding Object-Oriented Programming in JavaScript
Let's Understand How OOP makes our code more reusable, modular, and easier to maintain.

⤑ Object-Oriented Programming (OOP) is a programming paradigm used to structure code in a way that models real-world entities.
Instead of writing scattered functions and variables, OOP helps us organize code into objects and classes.
JavaScript supports OOP concepts, making code more reusable, modular, and easier to maintain.
❊ What is Object-Oriented Programming?
⤑ Object-Oriented Programming is a way of writing code where we create objects that represent real-world entities.
These objects contain:
• Properties → data about the object
• Methods → actions the object can perform
❊ Example: Car Object
Instead of listing properties and methods separately, we can visualize a Car object that contains both data (properties) and behavior (methods).
In this example, the Car object has:
Properties (data)
• brand
• color
• speed
Methods (behavior)
• start()
• stop()
• accelerate()
Instead of writing separate variables and functions, Object-Oriented Programming groups them together inside a single object.
❊ Real World Analogy: Blueprint → Objects
Think of a class as a blueprint.
Example:
Blueprint → Car Design
Object → Actual Car
One blueprint can create many cars.
❊ What is a Class in JavaScript?
⤑ A class is a template used to create objects.
It defines the properties and methods that objects will have.
Example:
class Car {
constructor(brand, color) {
this.brand = brand;
this.color = color;
}
start() {
console.log(this.brand + " car started");
}
}
Here:
Car → class
brand, color → properties
start() → method
❊ Creating Objects from a Class
⤑ Once we define a class, we can create objects using the new keyword.
Example:
const car1 = new Car("Toyota", "Red");
const car2 = new Car("BMW", "Black");
car1.start();
car2.start();
Output:
Toyota car started
BMW car started
Each object has its own data but shares the same structure.
❊ Constructor Method
⤑ The constructor() method is a special method used to initialize object properties.
It automatically runs when a new object is created.
Example:
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
}
const p1 = new Person("Rahul", 22);
console.log(p1.name);
Output:
Rahul
The constructor helps us assign values when creating objects.
❊ Methods Inside a Class
⤑ Methods define the behavior of objects.
Example:
class Dog {
constructor(name) {
this.name = name;
}
bark() {
console.log(this.name + " is barking");
}
}
const dog1 = new Dog("Rocky");
dog1.bark();
Output:
Rocky is barking
Methods allow objects to perform actions.
❊ Basic Idea of Encapsulation
⤑ Encapsulation means bundling data and methods together inside a class.
It helps protect data and organize code.
Example:
class BankAccount {
constructor(balance) {
this.balance = balance;
}
deposit(amount) {
this.balance += amount;
}
getBalance() {
return this.balance;
}
}
Here:
balance → data
deposit() → modifies data
getBalance() → accesses data
All related functionality is inside the class.
❊ Why Use Object-Oriented Programming?
OOP helps developers:
• Organize code better
• Reuse code easily
• Reduce duplication
• Model real-world systems
• Maintain large applications
Many modern frameworks and applications rely on OOP concepts.
❊ Conclusion
⤑ Object-Oriented Programming is an important concept in JavaScript that helps structure code effectively.
In this blog we learned:
• What Object-Oriented Programming means
• Blueprint → Object analogy
• What classes are in JavaScript
• Creating objects using classes
• Constructor methods
• Methods inside classes
• Basic idea of encapsulation
Understanding OOP will help you write clean, scalable, and maintainable JavaScript applications.
❊ Want More…?
I write articles on blog.prakashtsx.com and also post development-related content on the following platforms:



