# Understanding Object-Oriented Programming in JavaScript

⤑ Object-Oriented Programming (OOP) is a programming paradigm used to structure code in a way that models real-world entities.

Instead of writing scattered functions and variables, OOP helps us organize code into **objects and classes**.

JavaScript supports OOP concepts, making code more **reusable, modular, and easier to maintain**.

![](https://media.geeksforgeeks.org/wp-content/uploads/20250722184505270450/Types-of-OOPS-2.gif align="center")

* * *

## ❊ What is Object-Oriented Programming?

⤑ Object-Oriented Programming is a way of writing code where we create **objects that represent real-world entities**.

These objects contain:

• **Properties** → data about the object  
• **Methods** → actions the object can perform

### ❊ Example: Car Object

Instead of listing properties and methods separately, we can visualize a **Car object** that contains both **data (properties)** and **behavior (methods)**.

![](https://cdn.hashnode.com/uploads/covers/656ed88ac1f28c5dd219df1a/49fd8e14-b4cf-4198-a968-206336e82fd1.png align="center")

In this example, the **Car object** has:

**Properties (data)**  
• brand  
• color  
• speed

**Methods (behavior)**  
• start()  
• stop()  
• accelerate()

Instead of writing separate variables and functions, Object-Oriented Programming groups them together inside a single **object**.

* * *

## ❊ Real World Analogy: Blueprint → Objects

Think of a **class as a blueprint**.

Example:

Blueprint → Car Design  
Object → Actual Car

One blueprint can create **many cars**.

![](https://cdn.hashnode.com/uploads/covers/656ed88ac1f28c5dd219df1a/7ff788f3-0256-4a8f-af58-b4eeff4594f9.png align="center")

* * *

## ❊ What is a Class in JavaScript?

⤑ A **class** is a template used to create objects.

It defines the **properties and methods** that objects will have.

Example:

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

  start() {
    console.log(this.brand + " car started");
  }
}
```

Here:

***Car → class  
brand, color → properties  
start() → method***

* * *

## ❊ Creating Objects from a Class

⤑ Once we define a class, we can create objects using the **new keyword**.

Example:

```javascript
const car1 = new Car("Toyota", "Red");
const car2 = new Car("BMW", "Black");

car1.start();
car2.start();
```

Output:

```plaintext
Toyota car started
BMW car started
```

Each object has its **own data** but shares the same structure.

* * *

## ❊ Constructor Method

⤑ The **constructor()** method is a special method used to initialize object properties.

It automatically runs when a new object is created.

Example:

```javascript
class Person {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }
}

const p1 = new Person("Rahul", 22);
console.log(p1.name);
```

Output:

```plaintext
Rahul
```

The constructor helps us assign values when creating objects.

* * *

## ❊ Methods Inside a Class

⤑ Methods define the **behavior of objects**.

Example:

```javascript
class Dog {
  constructor(name) {
    this.name = name;
  }

  bark() {
    console.log(this.name + " is barking");
  }
}

const dog1 = new Dog("Rocky");
dog1.bark();
```

Output:

```plaintext
Rocky is barking
```

Methods allow objects to **perform actions**.

* * *

## ❊ Basic Idea of Encapsulation

⤑ Encapsulation means **bundling data and methods together inside a class**.

It helps protect data and organize code.

![](https://cdn.hashnode.com/uploads/covers/656ed88ac1f28c5dd219df1a/e878a4df-4a43-4ea6-9d86-5803818149ad.png align="center")

Example:

```javascript
class BankAccount {
  constructor(balance) {
    this.balance = balance;
  }

  deposit(amount) {
    this.balance += amount;
  }

  getBalance() {
    return this.balance;
  }
}
```

Here:

***balance → data  
deposit() → modifies data  
getBalance() → accesses data***

All related functionality is inside the class.

* * *

## ❊ Why Use Object-Oriented Programming?

OOP helps developers:

• Organize code better  
• Reuse code easily  
• Reduce duplication  
• Model real-world systems  
• Maintain large applications

Many modern frameworks and applications rely on OOP concepts.

* * *

## ❊ Conclusion

⤑ Object-Oriented Programming is an important concept in JavaScript that helps structure code effectively.

In this blog we learned:

• What Object-Oriented Programming means  
• Blueprint → Object analogy  
• What classes are in JavaScript  
• Creating objects using classes  
• Constructor methods  
• Methods inside classes  
• Basic idea of encapsulation

Understanding OOP will help you write **clean, scalable, and maintainable JavaScript applications**.

* * *

### **❊ Want More…?**

I write articles on [**blog.prakashtsx.com**](http://blog.prakashtsx.com) and also post development-related content on the following platforms:

*   [**Twitter/X**](https://x.com/prakashtsx)
    
*   [**LinkedIn**](https://www.linkedin.com/in/prakashtsx)
