Understanding Object-Oriented Programming in JavaScript (OOP)
A Beginner’s Guide

Let’s be real for a second. When you first hear the phrase Object-Oriented Programming (or OOP for short), it sounds incredibly intimidating. It sounds like something only senior engineers with five monitors talk about.
But honestly? It’s just a way of organizing your code so it doesn't turn into a giant, messy bowl of spaghetti. Instead of writing a bunch of random, disconnected functions, OOP lets us model our code after real-world things.
If you understand how a blueprint works, you already understand the core of OOP. Let’s break it down.
The Blueprint Analogy: Classes vs. Objects
Imagine you’re an engineer at a car factory. You don't just start welding metal together hoping a car pops out. First, you create a blueprint.
That blueprint defines exactly what the car is: it will have a color, a make, a model, and an engine. It also defines what the car can do: it can start, stop, and accelerate.
The Blueprint = The Class: The blueprint itself isn’t a car. You can’t drive a piece of paper. It’s just the template.
The Physical Cars = The Objects: Once the blueprint is done, the factory can use it to build hundreds of actual, drivable cars. You can paint one red and one black, but they both share the exact same underlying structure.
In JavaScript, a Class is the blueprint, and an Object is the actual physical "thing" you create from that blueprint.
What is a Class in JavaScript?
For a long time, JavaScript did OOP a little weirdly. But recently (well, since 2015), JavaScript gave us the class keyword, making everything much easier to read.
A class is basically a container that holds two things:
Properties: The data (like a car's color, or a student's name).
Methods: The actions it can take (like a car braking, or a student studying).
The constructor: Your Setup Crew
When you create a brand-new object from your class, it usually needs some starting information.
Think of the constructor method as the setup crew. It’s a special function built into your class that runs automatically the second a new object is created. Its only job is to take the initial data you provide and attach it to the new object.
Methods: Making Things Happen
If properties are the "nouns" (name, age), methods are the "verbs." These are just regular functions that live strictly inside your class. They allow your objects to actually do things with the data they hold.
Let's Write Some Code
Let’s step away from cars for a second and build a Student class. We want to be able to create multiple students without having to rewrite the same code over and over again. Notice how much time this saves us!
JavaScript
// 1. The Blueprint
class Student {
// 2. The Setup Crew (Constructor)
// This runs immediately when a new student is created
constructor(name, age) {
this.name = name; // 'this' just means "this specific student we are making right now"
this.age = age;
}
// 3. The Action (Method)
// Every student we create will know how to do this
printDetails() {
console.log(`Hi, my name is \({this.name} and I am \){this.age} years old.`);
}
}
// 4. Creating the actual Objects!
// We use the 'new' keyword to tell JS to look at the blueprint.
const studentOne = new Student("Aarav", 20);
const studentTwo = new Student("Priya", 22);
const studentThree = new Student("Rohan", 19);
// 5. Putting them to work
studentOne.printDetails(); // Output: Hi, my name is Aarav and I am 20 years old.
studentTwo.printDetails(); // Output: Hi, my name is Priya and I am 22 years old.
studentThree.printDetails(); // Output: Hi, my name is Rohan and I am 19 years old.
Look at that code reusability! We only wrote the printDetails() logic once inside the blueprint, but all three of our student objects get to use it.
A Quick Word on Encapsulation
If you look at our Student class, notice how the data (the name and age) and the behavior (the printing) are bundled up nicely together in one single package.
In the programming world, this is called Encapsulation. It’s a fancy word, but the basic idea is just keeping related things together and protecting an object's internal data from the rest of your app. It keeps your code predictable, organized, and out of trouble.
Conclusion
Conclusion
Object-oriented programming isn’t some mystical ritual only for senior engineers it’s a simple way to organize code by modeling real-world things. Think of a class as the blueprint and objects as the actual items you build from that blueprint.
In JavaScript you can express these ideas with ES6 class syntax, constructor functions, or directly with prototypes, but the goal is the same: group related data and behavior together so your code is easier to reason about, reuse, and maintain.
The core OOP benefits—encapsulation, clearer structure, and the ability to share or extend behavior—help prevent your project from turning into a “giant, messy bowl of spaghetti.”
Use OOP where it maps naturally to the problem domain, and don’t be afraid to mix in functional patterns when they make sense.
You are all set!
written by theadroitdev(Shivam Verma) :)
X → https://x.com/theadroitdev
Github → https://github.com/TheAdroitDev
ChaiCode Repo → https://github.com/TheAdroitDev/ChaiCode-Cohort-2026


