
Subhrangsu BeraUp until now, you’ve likely been writing code "procedurally"—writing a line, then another, then...
Up until now, you’ve likely been writing code "procedurally"—writing a line, then another, then another. It’s like keeping a recipe on a loose scrap of paper. But what happens when you want to open a global chain of restaurants? You can't rely on scraps of paper. You need a System.
In JavaScript, Object-Oriented Programming (OOP) is that system. It allows us to model our code after the real world.
The best way to understand OOP is the Car Factory analogy.
Imagine you are the CEO of a car company. You don't sit down and design every single car from scratch. Instead, you hire engineers to create one perfect Blueprint.
A Class is a reserved keyword in JavaScript (introduced in ES6) that acts as the template for creating objects. It groups data (properties) and behavior (methods) together so they don't get lost.
Before Classes, our code was "scattered":
let student1Name = "Veer";
let student1Age = 20;
let student2Name = "Ansh";
let student2Age = 22;
// This gets messy fast!
Every class has a special method called a constructor. Think of this as the Assembly Line. The moment you decide to create a new object, the constructor runs to set the initial values.
class Student {
// The constructor is the 'Setup' function
constructor(name, age, course) {
this.name = name; // 'this' refers to the specific student being built
this.age = age;
this.course = course;
}
// This is a Method (Action)
introduce() {
console.log(`Hi, I'm ${this.name}, a ${this.age}-year-old studying ${this.course}.`);
}
}
Why use this?
Inside a class, this is like a pointing finger. It says, "Take the name I just gave you and stick it onto this specific student object."
new Keyword
Now for the magic. To create an actual object from your blueprint, you use the new keyword. This process is called Instantiation.
// Manufacturing two unique students from one blueprint
const student1 = new Student("Veer", 20, "Web Dev");
const student2 = new Student("Ansh", 22, "Data Science");
// They both have the same "actions"
student1.introduce(); // Hi, I'm Veer...
student2.introduce(); // Hi, I'm Ansh...
One of the "Four Pillars" of OOP is Encapsulation.
Think of a Capsule (like a medicine pill). Inside the pill is the medicine (data) and the formula (logic). You don't need to know how the formula works to take the pill; you just need to know it works.
In our code, Encapsulation means we hide the complex logic inside the class. The rest of your program doesn't need to know how introduce() works; it just calls the method. This makes your code:
| Feature | Procedural (Old Way) | OOP (The Modern Way) |
|---|---|---|
| Logic | Functions and data are separate. | Data and logic are bundled together. |
| Scaling | Hard to manage as the app grows. | Designed for large, complex apps. |
| Analogy | A list of instructions. | A collection of interacting "things." |
To really let this sink in, open your console and try this "Blueprint" challenge:
Book.title, author, and year.getAge that calculates how old the book is (Current Year - Year)..getAge() for both.OOP is more than just syntax; it’s a mindset. It teaches you to stop thinking about your code as a list of tasks and start thinking about it as a world of "Objects" that interact with each other.
Once you master this, you can build anything—from a simple Todo app to a massive social media platform.