Object-Oriented Programming (OOP) in JavaScript

JavaScript supports Object-Oriented Programming (OOP), which is a way of organizing code so that it becomes easier to manage, reuse, and understand. Instead of writing scattered code, OOP helps us structure our programs using objects and classes that represent real-world things.
In this blog, we will understand:
What Object-Oriented Programming means
Real-world analogy (Blueprint → Objects)
What a class is in JavaScript
Creating objects using classes
Constructor method
Methods inside a class
Basic idea of encapsulation
Some additional concepts
What Object-Oriented Programming (OOP) Means?
Object-Oriented Programming (OOP) is a programming style where we organize code using objects that represent real-world entities.
These objects contain:
Properties (data)
Methods (functions)
Example: A Car
A car has properties like:
brand
color
speed
And actions like:
start()
drive()
brake()
In programming, we represent this using objects.
OOP helps developers:
Organize complex code
Reuse code easily
Model real-world systems
Real-World Analogy (Blueprint → Objects)
To understand OOP easily, think about building a house.
Before building houses, architects create a blueprint.
A blueprint defines:
number of rooms
structure
design
Using that blueprint, many houses can be built.
In programming:
| Real World | Programming |
|---|---|
| Blueprint | Class |
| House | Object |
Example:
A Car class is a blueprint.
From that blueprint we can create many cars:
BMW
Audi
Toyota
Each one is an object created from the class.
What is a Class in JavaScript
A class in JavaScript is a template or blueprint used to create objects.
It defines:
properties
methods
Example:
class Car {
constructor(brand, model) {
this.brand = brand;
this.model = model;
}
}
Here:
Caris a classIt defines how car objects should look.
Creating Objects Using Classes
Once a class is created, we can create objects using the new keyword.
Example:
class Car {
constructor(brand, model) {
this.brand = brand;
this.model = model;
}
}
let car1 = new Car("Toyota", "Fortuner");
let car2 = new Car("BMW", "X5");
Here:
car1andcar2are objectsBoth are created from the Car class
Output:
car1 → Toyota Fortuner
car2 → BMW X5
This shows how one class can create many objects.
Constructor Method
The constructor is a special method inside a class that runs automatically when an object is created.
It is used to initialize object properties.
Example:
class Student {
constructor(name, age) {
this.name = name;
this.age = age;
}
}
let s1 = new Student("Rahul", 20);
What happens here:
Object
s1is created.Constructor runs automatically.
Values are assigned.
So the object becomes:
s1 = { name: "Rahul", age: 20 }
Important rule:
A class can have only one constructor.
Methods Inside a Class
Classes can also contain methods (functions) that describe behaviors of objects.
Example:
class Person {
constructor(name) {
this.name = name;
}
greet() {
console.log("Hello " + this.name);
}
}
let p1 = new Person("Rahul");
p1.greet();
Output:
Hello Rahul
Here:
greet()is a method inside the classIt uses
thisto access object properties.
Understanding this Inside Classes
Inside a class:
this refers to the current object.
Example:
class User {
constructor(name) {
this.name = name;
}
showName() {
console.log(this.name);
}
}
let user1 = new User("Amit");
user1.showName();
Output:
Amit
Here this.name refers to user1's name.
Basic Idea of Encapsulation
Encapsulation means keeping data and methods together and controlling access to them.
In simple words:
Encapsulation = data protection
Example idea:
A bank account should not allow anyone to change the balance directly.
Instead, we use methods like:
deposit()
withdraw()
Example:
class BankAccount {
constructor(balance) {
this.balance = balance;
}
deposit(amount) {
this.balance += amount;
}
withdraw(amount) {
this.balance -= amount;
}
}
let account = new BankAccount(1000);
account.deposit(500);
account.withdraw(200);
console.log(account.balance);
Output:
1300
Here:
Balance is controlled using methods.
Data and functionality are grouped together.
This is the basic concept of encapsulation.
Important OOP Concepts You Should Also Know
Even though we focused on basics, OOP has four main principles.
1 Encapsulation
Keeping data and methods together and restricting access.
2 Inheritance
One class can inherit properties from another class.
Example:
Car → ElectricCar
3 Polymorphism
Methods behaving differently depending on the object.
4 Abstraction
Hiding unnecessary details and showing only important functionality.
These concepts become more important in advanced JavaScript.
Simple Real Example of OOP
class Laptop {
constructor(brand, price) {
this.brand = brand;
this.price = price;
}
showDetails() {
console.log(this.brand + " costs " + this.price);
}
}
let l1 = new Laptop("HP", 60000);
let l2 = new Laptop("Dell", 70000);
l1.showDetails();
l2.showDetails();
Output:
HP costs 60000
Dell costs 70000
Here we used:
class
constructor
objects
methods
this keyword
Conclusion
Object-Oriented Programming helps developers structure programs in a clean and reusable way. By using classes and objects, we can model real-world systems in code.
Key takeaways:
OOP organizes code using objects
Classes act as blueprints
Objects are created using the new keyword
Constructor initializes object data
Classes contain methods for behavior
Encapsulation protects and organizes data
Once you understand these basics, you can move toward advanced concepts like inheritance, polymorphism, and abstraction, which are widely used in modern JavaScript development.
Thank you for reading this blog. I am open to feedback and suggestion.





