Understanding Object-Oriented Programming in JS
When people hear Object-Oriented Programming (OOP) for the first time, it sounds like some big scary computer science concept.
But in reality it's just a way to organize code so that it becomes easier to manage and reuse.
And JavaScript gives us tools to do that.
So let's understand OOP in a very simple way.
What Object-Oriented Programming Means
Object-Oriented Programming is a programming style where we structure our code using objects.
Instead of writing random functions and variables everywhere, we group data and behavior together inside objects.
In simple words:
Object = Data + Functions related to that data
This makes the code easier to manage, especially when projects grow bigger.
Real World Analogy (Blueprint → Objects)
The easiest way to understand OOP is with a blueprint analogy.
Imagine you are building cars.
First you create a blueprint that describes:
car name
color
engine type
speed
functions like start() or drive()
Now using that blueprint you can create many cars.
Example:
Car 1 → Red Ferrari
Car 2 → Black BMW
Car 3 → White Tesla
All of them follow the same blueprint but represent different objects.
In programming:
Blueprint → Class
Actual Car → Object
What is a Class in JavaScript
A class is basically a blueprint used to create objects.
It defines:
properties (data)
methods (functions)
Syntax
Plain Text
Copy
class ClassName {
constructor() {}
methodName() {}
}
Let's see an example.
Creating a Class
Example:
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
greet() {
console.log("Hello, my name is " + this.name);
}
}
Here:
Person→ class nameconstructor()→ runs when an object is creatednameandage→ propertiesgreet()→ method
Creating Objects from a Class
Once the blueprint is ready, we can create objects from it.
Example:
const person1 = new Person("Tejas", 20);
const person2 = new Person("Rudra", 21);
Now we have two objects.
Both follow the same blueprint but contain different values.
person1.greet(); // Hello, my name is Tejas
person2.greet(); // Hello, my name is Rudra
One class → many objects.
Just like one blueprint → many cars.
The Constructor Method
The constructor is a special method inside a class.
It runs automatically when a new object is created.
Its main job is to initialize properties.
Example:
class Car {
constructor(brand, color) {
this.brand = brand;
this.color = color;
}
}
Now when we create objects:
const car1 = new Car("BMW", "Black");
const car2 = new Car("Tesla", "White");
Each object gets its own values.
Methods Inside a Class
Methods are functions that belong to a class.
They describe what the object can do.
Example:
class Car {
constructor(brand) {
this.brand = brand;
}
drive() {
console.log(this.brand + " is driving");
}
}
Using it:
const car1 = new Car("BMW");
car1.drive(); // BMW is driving
Here:
drive()is a methodthis.brandrefers to the object calling it
Basic Idea of Encapsulation
Encapsulation simply means keeping related data and functions together.
Instead of writing separate variables and functions everywhere, we keep them inside a class.
Example without structure:
let name = "Tejas";
function greet() {
console.log("Hello " + name);
}
Now imagine you have 100 people.
Things will become messy very quickly.
Using classes solves that.
class Person {
constructor(name) {
this.name = name;
}
greet() {
console.log("Hello " + this.name);
}
}
Now each person has their own data and behavior.
Clean and reusable.
Why OOP is Useful
OOP helps in:
Code reusability
Better organization
Managing large applications
Instead of rewriting logic again and again, you just create new objects from the same class.
Lazy developers love this.
And good developers too.
Final Thoughts
Object-Oriented Programming is just a clean way of structuring code using objects and classes.
Remember the simple rule:
Class → Blueprint
Object → Actual instance
Once you understand this concept, building larger applications becomes much easier.
In upcoming topics you'll see more OOP concepts like:
inheritance
polymorphism
abstraction
But for now, play with classes, create objects, and experiment with them.
That's the best way to learn.
And as always, don't forget to enjoy coding.



