Skip to main content

Command Palette

Search for a command to run...

Understanding Object-Oriented Programming in JavaScript

Updated
•3 min read•View as Markdown
Understanding Object-Oriented Programming in JavaScript
D

Computer Engineering student | Full-Stack Developer (in progress) | DSA in C++ | AI-era Tech Explorer 🚀 Building skills, mindset, and systems to create impactful technology.

When you first hear “Object-Oriented Programming”, it sounds big and complicated.
But honestly, the core idea is very simple.

OOP is just a way of organizing code so that it feels closer to how we think in real life.

Let’s understand it step by step.


Start With a Real-Life Example

Imagine a car company.

Before making actual cars, they create a blueprint.

That blueprint defines:

  • color

  • engine type

  • number of wheels

  • features

Using that one blueprint, they can create 1000 cars.

Each car is separate, but all follow the same structure.

In OOP:

Blueprint → Class
Real car → Object

That’s the main idea.


What Does Object-Oriented Programming Mean?

Object-Oriented Programming (OOP) is a style of programming where we structure our code using objects.

Instead of writing random functions and variables everywhere, we group related data and behavior together.

So in simple words:

OOP = organizing code using classes and objects.


What is a Class in JavaScript?

A class is like a template or blueprint.

It defines:

  • what properties an object will have

  • what actions (methods) it can perform

A class does not represent a real thing by itself.
It is just a plan.


Creating Objects Using Classes

When you create an object from a class, that process is called instantiation.

Think of it like:

Class → instruction
Object → actual working thing created from that instruction

You can create multiple objects from the same class, and each object will have its own data.


The Constructor Method

Inside a class, there is a special method called constructor.

The constructor runs automatically when you create a new object.

It is mainly used to:

  • initialize properties

  • assign values like name, age, etc.

For example, if you are creating a Person class, the constructor can set name and age when the object is created.


Methods Inside a Class

Methods are functions written inside a class.

They define what an object can do.

Example:
If you have a Student class, a method could be:

  • printDetails

  • study

  • getGrade

So properties describe the object.
Methods define its behavior.


Basic Idea of Encapsulation

Encapsulation means keeping related data and methods together inside one structure.

Instead of:

  • storing name in one place

  • storing age somewhere else

  • writing functions separately

You keep everything inside one class.

This makes code:

  • cleaner

  • easier to manage

  • easier to reuse

That’s the practical benefit.


Why OOP is Useful

The biggest advantage of OOP is reusability.

Once you create a class, you can use it again and again.

Instead of rewriting the same structure for every new student, user, or car —
you create a class once and reuse it.

This saves time and keeps your code organized.