Understanding Object-Oriented Programming in JavaScript
Everything I learned about OOP in JavaScript — explained simply

Have you ever noticed that in real projects, you end up creating many objects that look almost identical? One user object, another user object, another one — all with the same properties but different values. Typing the same structure again and again is not smart. That is exactly the problem Object-Oriented Programming (OOP) solves.
In this article, we will cover what OOP means, how classes work, how to create objects from a class, how constructors and methods work, and the basic idea of encapsulation — all with simple examples. By the end, you will be writing cleaner, reusable JavaScript code.
What is Object-Oriented Programming?
Object-Oriented Programming is a way of writing code by organising it around objects — instead of writing a long list of instructions.
The core idea is simple:
You define a template once. Then you create as many objects from it as you need.
Each object gets its own data but shares the same structure and behaviour.
The Blueprint Analogy
The best way to understand OOP is through a real-world analogy.
Think about a car blueprint. The blueprint is not a car itself — it is just the design. It says every car will have a brand, a color, and a speed. From that one blueprint, you can manufacture hundreds of different cars.
In JavaScript:
The blueprint is called a Class
Each car made from it is called an Instance (or Object)
What is a Class in JavaScript?
A class is a template for creating objects. You define it once using the class keyword, and then use it to create as many objects as you need.
class Car {
// class body goes here
}
That is it - you have defined a class. Now let us make it useful.
The Constructor Method
The constructor is a special method inside a class. It runs automatically every time you create a new object from the class. This is where you set the initial properties.
class Car {
constructor(brand, color, speed) {
this.brand = brand;
this.color = color;
this.speed = speed;
}
}
What is 'this'?
this refers to the current object being created. When you write this.brand = brand, you are saying — store the brand value on this particular object.
Creating Objects from a Class
You use the new keyword to create an object (instance) from a class.
One class — three completely different objects. This is the power of OOP. You write the template once and reuse it as many times as needed.
Methods Inside a Class
A method is a function defined inside a class. It gives your objects behaviour — things they can do.
Every object created from car automatically has access to describe() and accelerate(). You do not have to write those functions again for car2 or car3 — they just work.
Try it: Create
car2and callcar2.describe(). Notice how it uses car2's own data, not car1's — that isthisdoing its job!
Basic Idea of Encapsulation
Encapsulation means keeping related data and behaviour bundled together inside one class — and hiding internal details from the outside.
The car example already shows this. The speed property and the accelerate() method are both inside the Car class. The outside world just calls car1.accelerate(30) -> it does not need to know how it works internally.
Think of it like a TV remote — you press a button (method) without needing to know the electronics inside (internal logic). That is encapsulation in simple terms.
Putting It All Together
Let us now build a Student class from scratch — using everything we just learned.
Quick Reference Summary
What is OOP?
A way of writing code organised around objects — using classes as templates.
What is a Class?
A blueprint/template defined using the class keyword. Used to create multiple objects with the same structure.
What is a Constructor?
A special method that runs automatically when you create a new object. Used to set initial property values.
What is this ?
Refers to the current object being created or used. this.name means — the name property on this particular object.
What is a Method?
A function defined inside a class. Gives objects behaviour — things they can do.
What is Encapsulation?
Bundling related data and methods together inside a class. Keeps things organized and hides internal details.
What is an Instance?
An object created from a class using the new keyword.
Conclusion
OOP makes your code organized, reusable, and easier to maintain. Instead of writing the same object structure over and over, you define it once in a class and create as many instances as you need.
The key things to remember — a class is the blueprint, the constructor sets up the data, methods define the behaviour, and new creates the actual object.
Open VS Code, build the Student class from the assignment, create 5 different students, and call their methods. The best way to understand OOP is to build with it!