# Understanding Object-Oriented Programming in JavaScript

Have you ever noticed that in real projects, you end up creating many objects that look almost identical? One user object, another user object, another one — all with the same properties but different values. Typing the same structure again and again is not smart. That is exactly the problem **Object-Oriented Programming (OOP)** solves.

In this article, we will cover **what OOP means, how classes work, how to create objects from a class, how constructors and methods work, and the basic idea of encapsulation** — all with simple examples. By the end, you will be writing cleaner, reusable JavaScript code.

## What is Object-Oriented Programming?

Object-Oriented Programming is a way of writing code by organising it around **objects** — instead of writing a long list of instructions.

The core idea is simple:

> You define a template once. Then you create as many objects from it as you need.

Each object gets its own data but shares the same structure and behaviour.

## The Blueprint Analogy

The best way to understand OOP is through a real-world analogy.

Think about a **car blueprint**. The blueprint is not a car itself — it is just the design. It says every car will have a brand, a color, and a speed. From that one blueprint, you can manufacture hundreds of different cars.

![](https://cdn.hashnode.com/uploads/covers/696f304bc3ffcbb856137e02/0868e582-d5a8-46fd-b938-b76a562367ed.png align="center")

In JavaScript:

*   The **blueprint** is called a **Class**
    
*   Each **car** made from it is called an **Instance** (or Object)
    

## What is a Class in JavaScript?

A class is a **template** for creating objects. You define it once using the **class** keyword, and then use it to create as many objects as you need.

```javascript
class Car {
  // class body goes here
}
```

That is it - you have defined a class. Now let us make it useful.

## The Constructor Method

The **constructor** is a special method inside a class. It runs **automatically** every time you create a new object from the class. This is where you set the initial properties.

```javascript
class Car {
  constructor(brand, color, speed) {
    this.brand = brand;
    this.color = color;
    this.speed = speed;
  }
}
```

## What is 'this'?

**this** refers to the **current object** being created. When you write this.brand = brand, you are saying — store the brand value on this particular object.

![](https://cdn.hashnode.com/uploads/covers/696f304bc3ffcbb856137e02/72fbaf2c-bab4-430f-aa5a-40bbdc78c46c.png align="center")

## Creating Objects from a Class

You use the **new** keyword to create an object (instance) from a class.

![](https://cdn.hashnode.com/uploads/covers/696f304bc3ffcbb856137e02/9dbc68f0-92d0-4095-aeaa-7e7ad4958916.png align="center")

**One class — three completely different objects.** This is the power of OOP. You write the template once and reuse it as many times as needed.

![](https://cdn.hashnode.com/uploads/covers/696f304bc3ffcbb856137e02/6e915c3c-8da5-4d1f-b51c-79b1f913805c.png align="center")

## Methods Inside a Class

A **method** is a function defined inside a class. It gives your objects behaviour — things they can do.

![](https://cdn.hashnode.com/uploads/covers/696f304bc3ffcbb856137e02/28622121-7245-4dc3-8530-36c06ff0c341.png align="center")

Every object created from **car** automatically has access to **describe() and accelerate().** You do not have to write those functions again for car2 or car3 — they just work.

> **Try it:** Create `car2` and call `car2.describe()`. Notice how it uses car2's own data, not car1's — that is `this` doing its job!

## Basic Idea of Encapsulation

**Encapsulation** means keeping related data and behaviour **bundled together** inside one class — and hiding internal details from the outside.

The car example already shows this. The **speed** property and the **accelerate()** method are both inside the **Car** class. The outside world just calls **car1.accelerate(30) ->** it does not need to know how it works internally.

![](https://cdn.hashnode.com/uploads/covers/696f304bc3ffcbb856137e02/c6306e19-e139-401c-ac5c-40759ada4b33.png align="center")

Think of it like a TV remote — you press a button (method) without needing to know the electronics inside (internal logic). That is encapsulation in simple terms.

## Putting It All Together

Let us now build a Student class from scratch — using everything we just learned.

![](https://cdn.hashnode.com/uploads/covers/696f304bc3ffcbb856137e02/676e1cd3-f854-439f-8776-c10e059a99ed.png align="center")

## Quick Reference Summary

### What is OOP?

A way of writing code organised around objects — using classes as templates.

### What is a Class?

A blueprint/template defined using the **class** keyword. Used to create multiple objects with the same structure.

### What is a Constructor?

A special method that runs automatically when you create a new object. Used to set initial property values.

### What is this ?

Refers to the current object being created or used. [this.name](http://this.name) means — the name property on this particular object.

### What is a Method?

A function defined inside a class. Gives objects behaviour — things they can do.

### What is Encapsulation?

Bundling related data and methods together inside a class. Keeps things organized and hides internal details.

### What is an Instance?

An object created from a class using the **new** keyword.

## Conclusion

OOP makes your code **organized, reusable, and easier to maintain**. Instead of writing the same object structure over and over, you define it once in a class and create as many instances as you need.

The key things to remember — a class is the blueprint, the constructor sets up the data, methods define the behaviour, and **new** creates the actual object.

Open VS Code, build the Student class from the assignment, create 5 different students, and call their methods. The best way to understand OOP is to build with it!
