Skip to main content

Command Palette

Search for a command to run...

Understanding Object Oriented Programming in JavaScript

📦Understanding how JavaScript objects and classes help structure real world code 🚗

Published
6 min read
Understanding Object Oriented Programming in JavaScript

When people start learning programming, most code they write is a sequence of instructions. The program runs line by line and performs tasks step by step. This approach works well for small programs, but as applications grow larger it becomes harder to manage everything in a simple sequence.

That is where Object Oriented Programming becomes useful. It helps organize code into meaningful structures so that large programs become easier to build and maintain.

Object Oriented Programming, often called OOP, is a way of writing programs using objects that represent real world things. Instead of thinking only about functions and variables, we think about entities such as a student, a car, or a user.

Each object contains data and behavior related to that entity. The data describes the object and the behavior defines what the object can do.

Before diving into code, it helps to think about a real world example.

A Simple Real World Analogy

Imagine a company that manufactures cars.

Before any car is produced, engineers first design a blueprint. The blueprint defines what a car should look like and what properties it should have. For example the blueprint might describe things like the brand, color, and engine type.

Using that blueprint, the company can create many cars. Each car follows the same design but has its own specific values.

One car might be red while another is blue. One might have a different engine model. Even though they are different cars, they are all created from the same blueprint.

Object Oriented Programming follows a very similar idea.

The blueprint is called a class. The actual cars created from that blueprint are called objects.

Once we define a class, we can create multiple objects from it. This makes our code reusable and organized.

What is a Class in JavaScript

In JavaScript, a class is a template used to create objects. It defines the properties and methods that objects created from it will have.

Let us look at a small example of a class.

class Person {

    constructor(name, age) {
        this.name = name
        this.age = age
    }

    introduce() {
        console.log("Hi, my name is " + this.name + " and I am " + this.age + " years old.")
    }

}

In this example we created a class called Person.

Inside the class we defined a constructor and a method called introduce.

The constructor is responsible for assigning initial values when an object is created.

The introduce method describes behavior that the object can perform.

Now that the blueprint exists, we can create objects from it.

Creating Objects Using Classes

Creating an object from a class is called instantiation.

To create an object we use the new keyword followed by the class name.

Here is how we can create two person objects.

let person1 = new Person("Rahul", 22)
let person2 = new Person("Meera", 25)

person1.introduce()
person2.introduce()

Output

Hi, my name is Rahul and I am 22 years old.

Hi, my name is Meera and I am 25 years old.

Both objects were created using the same class. The structure is identical but the values are different.

This is exactly like creating multiple cars using the same blueprint.

Understanding the Constructor Method

The constructor method plays a very important role in classes.

It runs automatically whenever a new object is created.

Its purpose is to initialize properties of the object.

Look again at the constructor inside the Person class.

constructor(name, age) {
    this.name = name
    this.age = age
}

When we create a new object like this

Person("Rahul", 22)

the constructor receives the values Rahul and 22 and assigns them to the object.

The keyword this refers to the current object being created.

So this.name stores the name value inside that object.

Methods Inside a Class

Methods are functions that belong to a class.

They define what an object can do.

In the Person class we added a method called introduce. This method prints information about the person.

Let us look at another example using a Car class.

class Car {

    constructor(brand, model) {
        this.brand = brand
        this.model = model
    }

    showDetails() {
        console.log("Car brand is " + this.brand + " and model is " + this.model)
    }

}

let car1 = new Car("Toyota", "Corolla")
car1.showDetails()

Output

Car brand is Toyota and model is Corolla

Here the showDetails method describes behavior associated with the car object.

This is one of the strengths of Object Oriented Programming. Data and behavior related to an entity stay together.

Basic Idea of Encapsulation

Another important concept in OOP is encapsulation.

Encapsulation simply means keeping related data and functions together in one place.

In our earlier examples the class contains properties and methods that belong to the same concept.

The Person class stores name and age and also contains behavior related to a person.

The Car class stores brand and model and contains behavior related to a car.

Instead of scattering logic throughout the program, we group everything inside classes.

This makes programs easier to read and maintain.

Encapsulation also helps protect data by controlling how it is accessed, although deeper concepts of data protection are usually learned later.

For beginners it is enough to understand that classes help keep related code organized.

Why OOP Helps in Real Projects

Object Oriented Programming provides several advantages when building larger programs.

First, it promotes code reuse. Once a class is written, we can create many objects without rewriting the same logic again.

Second, it makes code easier to understand because each class represents a clear concept.

Third, it helps maintain large applications since related behavior stays grouped together.

For example, if a project manages hundreds of students, having a Student class keeps all student related information and behavior in one place.

Without OOP, managing such systems becomes messy very quickly.

A Small Practice Exercise

To really understand OOP, it helps to create a class yourself.

Try creating a class called Student that stores the name and age of a student.

Then add a method that prints the student details.

Example solution

class Student {

    constructor(name, age) {
        this.name = name
        this.age = age
    }

    showDetails() {
        console.log("Student name: " + this.name + ", Age: " + this.age)
    }

}

let student1 = new Student("Arjun", 20)
let student2 = new Student("Sneha", 21)

student1.showDetails()
student2.showDetails()

Output

Student name: Arjun, Age: 20

Student name: Sneha, Age: 21

Both students were created using the same class. The structure is shared while the values are different.

This small example demonstrates how classes help create structured and reusable code.

Once you become comfortable with classes and objects, Object Oriented Programming starts to feel very natural. Many real world applications rely on these ideas to manage complex systems in an organized way.

Understanding this concept early will make it easier to explore more advanced JavaScript patterns in the future.

I hope this blog gave you the necessary insight needed.