Object-Oriented Programming in JavaScript: A Complete Guide

Object Oriented Programming (OOP) is a style of programming that uses classes and object to represent real world entity , object and that's property(data) and behaviors(methods) .
Instead of write scattered variable and function , OOP group them into object .
Traditional Approach (❌ Messy):
let personName = "John";
let personAge = 25;
let personCity = "Mumbai";
function printPerson() {
console.log(personName + " is " + personAge + " years old");
}
OOP Approach (✅ Organized):
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
printDetails() {
console.log(this.name + " is " + this.age + " years old");
}
}
const person1 = new Person("John", 25);
person1.printDetails();
🔧 Real-World Analogy: Blueprint → Objects
Imagine a car manufacture compnay:
A car Blueprint that :
Contains design, specifications
Used to manufacture many cars
Follows by each car
But each car is unique (different color, number)
Code :
class Car {
constructor(color, engineSize) {
this.color = color;
this.engineSize = engineSize;
}
honk() {
console.log("Beep beep!");
}
}
const car1 = new Car("Red", "2L"); // Car 1
const car2 = new Car("Blue", "2L"); // Car 2
const car3 = new Car("White", "3L"); // Car 3
console.log(car1.color); // Output: Red
console.log(car2.color); // Output: Blue
car1.honk(); // Output: Beep beep!
Key Concept
Class = Blueprint (template)
Object = Actual thing created from blueprint
Many objects can use the same class
What is Class
A class is a blueprint or template fir creating object .
Class Syntax:
class ClassName {
constructor(parameters) {
// Initialize properties
}
method() {
// Do something
}
}
Key Components
Class name - Always starts with capital letter (Person, Car, Student)
Constructor - Special method that runs when creating object
Properties - Variables inside class (name, age, color)
Methods - Functions inside class (greet, honk, study)
Creating Objects Using Classes
Create a class :
class Car {
constructor(brand, color) {
this.brand = brand;
this.color = color;
}
}
Create object from the class :
const car1 = new Car("Toyota", "Red");
const car2 = new Car("Honda", "Blue");
const car3 = new Car("BMW", "Black");
Access properties
console.log(car1.brand); // Output: Toyota
console.log(car2.color); // Output: Blue
console.log(car3.brand); // Output: BMW
⚠️ Important Notes
Class name must be capitalized (Person, not person)
Use
newkeyword to create objectsConstructor runs automatically when creating object
Each object is independent (changing one doesn't affect others)
What is Constructor?
A constructor is a special method that runs automatically when you create a new object.
Purpose:
Initialize properties
Set up default values
Prepare the object
Constructor Syntax
class ClassName {
constructor(param1, param2) {
this.property1 = param1;
this.property2 = param2;
}
}
What are Methods?
Methods are functions inside a class that can access and modify object properties.
Syntax:
class ClassName {
method1() {
// Code here
}
method2() {
// Code here
}
}
Encapsulation
Encapsulation is a concept in OOP that refer to how to hide the internal details of an object and exposing only necessary information to the outside world !
Real Life Example :
Coca-Cola 🥤
The formula for Coca‑Cola soft drink is one of the most famous secrets in the world.
Only a few people know the exact recipe.
The formula is kept in a vault in World of Coca‑Cola.
Factories only receive ingredients in prepared form, not the full recipe.
So:
| Concept | Example |
|---|---|
| Hidden (encapsulated) | The secret formula |
| Exposed | The drink you buy |
Example: Burger 🍔
Companies like Burger King and McDonald's also keep:
special sauce recipes
spice mixes
cooking processes
secret from the public.
Customers only see the final burger, not how the recipe is made.
How to relate in programming
class Car {
constructor(brand) {
this.brand = brand;
this.fuel = 100;
}
drive() {
if (this.fuel > 0) {
this.fuel = this.fuel - 10;
console.log("Driving...");
} else {
console.log("No fuel!");
}
}
checkFuel() {
console.log("Fuel: " + this.fuel);
}
}
const car = new Car("Toyota");
car.drive();
car.checkFuel();
User doesn't need to know how fuel works, just:
- Call drive()
- Call checkFuel()
May be this blog help you !
Happy Coding



