Understanding Object-Oriented Programming in JavaScript
From blueprints to living code—learn how to organize, scale, and reuse your JavaScript with classes and objects.

Search for a command to run...
From blueprints to living code—learn how to organize, scale, and reuse your JavaScript with classes and objects.

No comments yet. Be the first to comment.
These are the assignments or knowledge dumps I decide to write for the "Chai aur Code's web Dev Cohort 2026"
They look similar, but the JavaScript engine treats them entirely differently. Learn how functions work under the hood and why hoisting matters.
RAG helps LLMs answer questions using your own data, but it doesn't guarantee correct answers. Here's how the pipeline works and where it commonly breaks down.

Imagine asking an AI, "Write a blog," versus "Act as an SEO expert and write a 1,000-word blog on AI safety formatted in markdown." The difference in the resulting output is night and day. A prompt is

Spotify and Apple Music make it incredibly easy to showcase your live listening habits on your website. YouTube Music? Not so much. After doing some setup you can do an API call and fetch your account

Arthur C. Clarke's famous third law: "Any sufficiently advanced technology is indistinguishable from magic" perfectly captures how the pace of human innovation works. From carrying supercomputers in o

Why HTTP is amnesiac, the difference between transport and state, and how to choose the right auth strategy for your Node.js app.
As your JavaScript projects grow from simple scripts into full-fledged web applications, you will quickly realize that keeping track of dozens of unrelated variables and functions becomes a nightmare.
To solve this, developers use a specific style of writing code known as Object-Oriented Programming (OOP).
At its core, OOP is a programming paradigm based on the concept of "objects." Instead of treating data (variables) and actions (functions) as separate, unrelated entities, OOP packages them together into single, organized units.
If you want to write clean, modular, and highly reusable code, understanding OOP is non-negotiable. Here is your beginner-friendly guide to mastering the basics of Object-Oriented Programming in JavaScript.
Before we look at any code, let’s understand the core concept of OOP using a real-world analogy: Building a Car.
Imagine you work at a car manufacturing plant. You don't design a brand new car from scratch every single time an order comes in. That would be wildly inefficient. Instead, the engineers create a blueprint.
This blueprint defines what a car is:
Properties (Data): It has a color, a brand, and a top speed.
Behaviors (Actions): It can accelerate, brake, and honk.
The blueprint itself is not a physical car you can drive. It is simply the template. Using this single blueprint, the factory can produce thousands of actual, physical cars.
Car 1 might be a Red Toyota.
Car 2 might be a Blue Ford.
In Object-Oriented Programming:
The Blueprint is called a Class.
The Physical Cars are called Objects (or Instances).
[ Blueprint / CLASS ] =======> [ Object / INSTANCE 1 ]
Name: Car Brand: Toyota
Properties: Color, Brand Color: Red
Methods: Drive(), Brake()
=======> [ Object / INSTANCE 2 ]
Brand: Ford
Color: Blue
In JavaScript, a Class is the syntax we use to create our blueprints. Introduced in ES6 (ECMAScript 2015), the class keyword provides a clean, elegant way to set up your templates.
The primary benefit of a class is code reusability. Instead of manually writing out objects over and over again, you write the logic once inside a class, and then use that class to stamp out as many objects as you need.
Let’s step away from cars and build something you might actually use in a web application: a Student management system.
// 1. Defining the Blueprint (Class)
class Student {
// Setup goes here
}
Right now, our blueprint is empty. We need a way to assign properties (like name and age) whenever a new student is created. To do this, we use a special method called the Constructor.
The constructor() is a special, built-in function inside a class. It runs automatically the exact moment you create a new object from your class. Its main job is to set up the initial data for that specific object.
class Student {
// The constructor runs automatically when a new student is created
constructor(studentName, studentAge) {
this.name = studentName;
this.age = studentAge;
}
}
Notice the keyword this. In the context of a class, this refers to the specific object being created at that moment. It translates to: "Set the name of THIS specific student to whatever name was passed in."
Now that our blueprint is ready, let's create some actual students! To create an object from a class, we use the new keyword.
// Stamping out multiple objects from our single blueprint
const studentOne = new Student("Alice", 22);
const studentTwo = new Student("Bob", 24);
console.log(studentOne.name); // Output: Alice
console.log(studentTwo.age); // Output: 24
Thanks to our class, we didn't have to duplicate the object structure. We just passed the raw data into our Student template, and it generated beautifully formatted objects for us.
Data is great, but objects should also be able to do things. In OOP, functions that belong to a class are called Methods.
Let’s add a method to our Student class that allows the student to print their details and introduce themselves.
class Student {
constructor(studentName, studentAge) {
this.name = studentName;
this.age = studentAge;
}
// Adding a Method (Behavior)
introduce() {
console.log(`Hi, my name is \({this.name} and I am \){this.age} years old.`);
}
}
const studentOne = new Student("Alice", 22);
const studentTwo = new Student("Bob", 24);
// Calling the method on our objects
studentOne.introduce(); // Output: Hi, my name is Alice and I am 22 years old.
studentTwo.introduce(); // Output: Hi, my name is Bob and I am 24 years old.
Notice how clean this is? We defined the introduce() logic exactly once inside the class, but both studentOne and studentTwo have access to it. This is the power of OOP.
As you dive deeper into OOP, you will hear a lot of technical terms. The most important one to understand right now is Encapsulation.
Think of a medicinal pill—a capsule. The medicine is safely contained inside the shell. You don't need to know the complex chemical makeup of the powder inside; you just swallow the capsule, and it does its job.
In OOP, encapsulation is the concept of bundling data (variables) and methods (functions) together inside a single unit (a class).
By doing this, you hide the complex internal mechanics of your code from the outside world. When another developer uses your Student class, they don't need to understand how the introduce() method works under the hood. They just need to know that if they call studentOne.introduce(), it will print the right message.
Encapsulation keeps your code organized, protects your data from being messed with accidentally, and makes large applications much easier to maintain.
Object-Oriented Programming might feel like a steep learning curve at first, but it is entirely based on logic you already understand from the real world.
Classes are blueprints (templates).
Objects are the actual things created from those blueprints.
Constructors setup the initial data when an object is born.
Methods give your objects the ability to perform actions.
Encapsulation keeps your data and actions neatly bundled into a single, predictable package.
By utilizing classes, you are no longer just writing lines of code; you are building intelligent, reusable machines.