Understanding Object-Oriented Programming in JavaScript

Why Do Developers Use Object-Oriented Programming?
When you start writing small JavaScript programs, you often store data in variables or simple objects. But as your applications grow, you begin to notice a problem: a lot of your code starts repeating itself.
Imagine building a student management system. Every student has a name, age, and maybe a function that shows their details.
You might write something like this:
let student1 = { name: "Amit", age: 20 };
let student2 = { name: "Priya", age: 21 };
let student3 = { name: "Rahul", age: 22 };
This works, but if you have 50 or 100 students, repeating the same structure again and again becomes messy and hard to maintain.
This is where Object-Oriented Programming (OOP) helps. OOP allows us to create a blueprint for objects so we can easily generate multiple similar objects without rewriting the same code.
The Blueprint Analogy: Understanding the Core Idea
A helpful way to understand OOP is through a real-world analogy.
Think about how cars are manufactured. Before a car is built, engineers design a blueprint. This blueprint describes things like the engine, wheels, and body structure.
Once the blueprint exists, the factory can create many cars from it.
In programming, the idea is similar:
The blueprint is called a class
The actual cars created from the blueprint are called objects
So instead of manually defining each object, we create a class and generate many objects from it.
What is a Class in JavaScript?
A class in JavaScript is simply a template used to create objects. It describes what properties an object should have and what actions it can perform.
Here is the basic syntax:
class Student {
}
At this point, the class does not contain any data yet. It is just a structure that we will use to create objects later.
Classes help us organize code and define reusable structures for our objects.
Creating Objects from a Class
Once a class is defined, we can create objects from it using the new keyword. Each object created from a class is called an instance.
For example:
let student1 = new Student();
This creates a new object based on the Student class.
However, this object currently has no information such as name or age. To assign values when an object is created, we use something called a constructor.
The Constructor Method
The constructor is a special method inside a class. It runs automatically whenever a new object is created.
Its main job is to initialize properties for that object.
Example:
class Student {
constructor(name, age) {
this.name = name;
this.age = age;
}
}
Here’s what happens:
The constructor receives
nameandageas parameters.thisrefers to the current object being created.The values are stored as properties of that object.
Now we can create multiple students easily:
let student1 = new Student("Amit", 20);
let student2 = new Student("Priya", 21);
Each object now has its own data.
Methods Inside a Class
Classes can also contain methods, which are simply functions defined inside the class. Methods describe what an object can do.
For example, we might want each student to print their details.
class Student {
constructor(name, age) {
this.name = name;
this.age = age;
}
showDetails() {
console.log(this.name + " is " + this.age + " years old");
}
}
Now we can use this method with any student object:
let student1 = new Student("Amit", 20);
let student2 = new Student("Priya", 21);
student1.showDetails();
student2.showDetails();
Even though we created two different students, the same method works for both. This is one of the key benefits of OOP: code reusability.
The Basic Idea of Encapsulation
Encapsulation may sound complicated, but the idea is simple.
Encapsulation means keeping related data and functions together inside a class.
In the Student class:
nameandageare datashowDetails()is a function that works with that data
Both are grouped together inside the class.
You can think of encapsulation like a capsule that holds everything related in one place. This makes code easier to organize and understand.
Why OOP Matters?
Object-Oriented Programming helps developers structure their programs in a cleaner and more logical way. Instead of repeating code, we create reusable blueprints and generate objects whenever needed.
This approach makes programs easier to expand, maintain, and understand—especially in larger applications.





