# Understanding Object-Oriented Programming in JavaScript

# Object-Oriented Programming (OOP) in JavaScript — Beginner Guide

As programs grow bigger, managing data and behavior becomes harder.  
**Object-Oriented Programming (OOP)** helps organize code by modeling real-world entities using **objects and classes**. ([GeeksforGeeks](https://www.geeksforgeeks.org/es6-classes/?utm_source=chatgpt.com))

In this guide we will learn:

*   What OOP means
    
*   Real-world blueprint analogy
    
*   What a **class** is
    
*   Creating objects using classes
    
*   The **constructor** method
    
*   Methods inside classes
    
*   Basic idea of **encapsulation**
    

* * *

# 1\. What is Object-Oriented Programming?

Object-Oriented Programming is a way of structuring code using **objects that combine data and behavior**. ([GeeksforGeeks](https://www.geeksforgeeks.org/javascript-classes/?utm_source=chatgpt.com))

Instead of writing scattered variables and functions, OOP groups them together.

Example idea:

```plaintext
Car
 ├── color

 ├── speed

 └── start()
```

Here:

*   **data** → color, speed
    
*   **behavior** → start()
    

This approach makes programs easier to understand and maintain.

* * *

# 2\. Real-World Analogy: Blueprint → Objects

Think about building cars.

```plaintext
Car blueprint → many cars
```

Blueprint defines:

*   wheels
    
*   engine
    
*   color
    
*   speed
    

But the **actual cars are objects created from that blueprint**.

Programming works the same way.

```plaintext
Class → blueprint
Object → real instance
```

Example:

```plaintext
Class: Car
Objects: Car1, Car2, Car3
```

* * *

# 3\. What is a Class in JavaScript?

A **class is a blueprint for creating objects** with shared properties and methods. ([stanza.dev](http://stanza.dev))

JavaScript introduced classes in **ES6** to make object-oriented code easier to write and organize. ([GeeksforGeeks](https://www.geeksforgeeks.org/javascript/javascript-classes/?utm_source=chatgpt.com))

Basic syntax:

```plaintext
class ClassName {}
```

Example:

```plaintext
class Person {}
```

This class doesn’t do anything yet—it’s just the blueprint.

* * *

# 4\. Creating Objects Using Classes

To create an object from a class we use the **new keyword**. ([Medium](https://medium.com/%40ks.deepak07/es6-classes-in-javascript-e94a5db9fdb2?utm_source=chatgpt.com))

Example:

```plaintext
class Person {}
let person1 = new Person();
let person2 = new Person();
```

Now we have **two objects created from the same class**.

```plaintext
Person class
│
├── person1
└── person2
```

This is where **code reusability** comes from.

* * *

# 5\. The Constructor Method

The **constructor** is a special method that runs automatically when a new object is created. ([GeeksforGeeks](https://www.geeksforgeeks.org/javascript/javascript-classes/?utm_source=chatgpt.com))

It is used to **initialize properties**.

Example:

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

Create objects:

```plaintext
let p1 = new Person("Tushar", 22);
let p2 = new Person("Rahul", 25);
```

Now each object has its own values.

```plaintext
p1 → {name:"Tushar", age:22}
p2 → {name:"Rahul", age:25}
```

* * *

# 6\. Methods Inside a Class

Methods define **behavior** for objects.

Example:

```plaintext
class Person {
 constructor(name, age) {

    this.name = name;

    this.age = age;

  }

  introduce() {

    console.log("Hi my name is " + this.name);

  }

}
```

Create object and call method:

```plaintext
let p1 = new Person("Tushar", 22);
p1.introduce();
```

Output:

```plaintext
Hi my name is Tushar
```

Here:

```plaintext
constructor → sets data
method → performs action
```

* * *

# 7\. Basic Idea of Encapsulation

**Encapsulation means bundling data and methods together inside one structure.** ([GeeksforGeeks](https://www.geeksforgeeks.org/javascript-classes/?utm_source=chatgpt.com))

Example:

```plaintext
class Car {
constructor(speed) {

    this.speed = speed;

  }

  drive() {

    console.log("Driving at " + this.speed);

  }

}
```

Here the **data and behavior belong to the same object**.

```plaintext
Car object
├── speed
└── drive()
```

Encapsulation helps keep code **organized and maintainable**.

* * *

# Small Practical Example

### Car class

```plaintext
class Car {
constructor(brand, speed) {

    this.brand = brand;

    this.speed = speed;

  }

  drive() {

    console.log(this.brand + " is driving at " + this.speed);

  }

}
```

Create objects:

```plaintext
let car1 = new Car("BMW", 120);
let car2 = new Car("Audi", 140);

car1.drive();

car2.drive();
```

Output

```plaintext
BMW is driving at 120
Audi is driving at 140
One class → many objects.
```

* * *

# Class → Instance Diagram

```plaintext
        Class
        Person

           │

   -------------------

   │        │        │

 person1  person2  person3
```

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

* * *

# Quick Summary

<table style="min-width: 50px;"><colgroup><col style="min-width: 25px;"><col style="min-width: 25px;"></colgroup><tbody><tr><td colspan="1" rowspan="1"><p>Concept</p></td><td colspan="1" rowspan="1"><p>Meaning</p></td></tr><tr><td colspan="1" rowspan="1"><p>OOP</p></td><td colspan="1" rowspan="1"><p>Programming using objects</p></td></tr><tr><td colspan="1" rowspan="1"><p>Class</p></td><td colspan="1" rowspan="1"><p>Blueprint for objects</p></td></tr><tr><td colspan="1" rowspan="1"><p>Object</p></td><td colspan="1" rowspan="1"><p>Instance of a class</p></td></tr><tr><td colspan="1" rowspan="1"><p>Constructor</p></td><td colspan="1" rowspan="1"><p>Initializes object properties</p></td></tr><tr><td colspan="1" rowspan="1"><p>Method</p></td><td colspan="1" rowspan="1"><p>Function inside a class</p></td></tr><tr><td colspan="1" rowspan="1"><p>Encapsulation</p></td><td colspan="1" rowspan="1"><p>Bundling data and behavior</p></td></tr></tbody></table>

* * *

💡 **Key idea to remember**

```plaintext
Class → blueprint
```

Object → real instance

Constructor → sets initial data

Method → defines behavior
