Understanding Object-Oriented Programming in JavaScript
Modern software applications often model real-world entities like users, products, cars, or students. Writing separate variables for every property quickly becomes messy.
This is where Object-Oriented Programming (OOP) helps.
OOP allows us to structure code using objects that represent real-world entities, making programs easier to organize, reuse, and maintain.
In this article, we will understand how Object-Oriented Programming works in JavaScript using simple examples.
What is Object-Oriented Programming (OOP)?
Object-Oriented Programming (OOP) is a programming style where we organize code using objects.
An object is simply a collection of:
Properties → data about the object
Methods → actions the object can perform
Example
A Car object might have:
Properties:
color
brand
speed
Methods:
start()
stop()
accelerate()
Instead of writing many unrelated variables, OOP groups everything together inside an object.
This makes code cleaner, reusable, and easier to manage.
Real-World Analogy: Blueprint → Objects
To understand OOP, think about building cars in a factory.
Blueprint
A blueprint describes how a car should be built:
color
engine
model
wheels
But the blueprint itself is not a car.
Objects
Using the blueprint, the factory creates many cars:
Car 1
Car 2
Car 3
Each car follows the same blueprint but has its own values.
In programming:
| Real World | Programming |
|---|---|
| Blueprint | Class |
| Car | Object |
So:
Class → Blueprint
Object → Real instance created from the blueprint
What is a Class in JavaScript?
A class is a blueprint used to create objects.
It defines:
what properties an object will have
what methods an object can perform
Example
class Car {
constructor(brand, color) {
this.brand = brand;
this.color = color;
}
start() {
console.log(this.brand + " car started");
}
}
Here:
Car→ classbrandandcolor→ propertiesstart()→ method
Creating Objects Using Classes
To create objects from a class, JavaScript uses the new keyword.
Example
const car1 = new Car("Toyota", "Red");
const car2 = new Car("BMW", "Black");
Now we have two different objects:
| Object | Brand | Color |
|---|---|---|
| car1 | Toyota | Red |
| car2 | BMW | Black |
Both were created from the same Car class blueprint.
Constructor Method
The constructor is a special method inside a class.
It runs automatically when an object is created.
It is used to initialize properties of the object.
Example
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
}
When we create an object:
const person1 = new Person("Alice", 25);
JavaScript automatically calls the constructor and assigns:
name → Alice
age → 25
Methods Inside a Class
Methods are functions that belong to the class.
They define actions an object can perform.
Example
class Car {
constructor(brand) {
this.brand = brand;
}
drive() {
console.log(this.brand + " is driving");
}
}
Using the method:
const car1 = new Car("Tesla");
car1.drive();
Output:
Tesla is driving
Methods allow objects to perform behaviour, not just store data.
Mental model of class -> instance visual relation
Basic Idea of Encapsulation
Encapsulation means keeping data and methods together inside a class.
Instead of spreading logic across many functions and variables, everything related to an entity stays in one place.
Example:
class BankAccount {
constructor(owner, balance) {
this.owner = owner;
this.balance = balance;
}
deposit(amount) {
this.balance += amount;
}
}
Here the data (balance) and operation (deposit) are grouped inside the same class.
This improves:
code organization
maintainability
security
Assignment Example: Student Class
Let’s create a simple Student class.
Step 1: Create the class
class Student {
constructor(name, age) {
this.name = name;
this.age = age;
}
printDetails() {
console.log("Name: " + this.name);
console.log("Age: " + this.age);
}
}
Step 2: Create objects
const student1 = new Student("Rahul", 20);
const student2 = new Student("Ananya", 21);
Step 3: Use the method
student1.printDetails();
student2.printDetails();
Output:
Name: Rahul
Age: 20
Name: Ananya
Age: 21
Here we created multiple student objects from one class, which demonstrates code reusability.
Why OOP is Useful
Object-Oriented Programming helps developers:
Organize code better
Reuse code using classes
Model real-world entities easily
Build scalable applications
OOP is widely used in large applications, frameworks, and software systems.