Back to blog
JavaScript

Understanding Object-Oriented Programming in JavaScript

May 10, 2026 3 min read

What Is Object-Oriented Programming (OOP)?

OOP is a programming style where code is organized using:

  • Objects

  • Classes

  • Properties

  • Methods

It helps structure real-world data and behavior.


Real-World Analogy

Think about a car factory.

Blueprint → Creates Cars

The blueprint defines:

  • Color

  • Brand

  • Speed

Actual cars are created from that blueprint.

In JavaScript:

Real WorldJavaScript
BlueprintClass
CarObject

Blueprint → Object Diagram

Diagram: flowchart LR

What Is a Class?

A class is a blueprint for creating objects.


Basic Class Example

class Person {
}


Creating Objects From a Class

Use:

new


Example

class Person {
}

const user1 = new Person();

console.log(user1);


What Is a Constructor?

The constructor initializes object properties when an object is created.


Example

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


Creating Object

const user1 = new Person("Ali", 22);

console.log(user1);


Output

Person { name: 'Ali', age: 22 }


How Constructor Works

flowchart TD
    A[new Person()] --> B[constructor()]
    B --> C[Object Created]


What Are Methods?

Methods are functions inside a class.

They define object behavior.


Example

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

  greet() {
    console.log(`Hello, my name is ${this.name}`);
  }
}


Using Method

const user1 = new Person("Sara", 20);

user1.greet();


Output

Hello, my name is Sara


Class → Instance Relationship

Diagram: flowchart LR

Multiple Objects From Same Class

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

const car1 = new Car("Toyota", "Black");
const car2 = new Car("Honda", "White");

console.log(car1);
console.log(car2);


Why OOP Is Useful

BenefitExplanation
ReusabilityReuse same class
OrganizationCleaner structure
ScalabilityEasier large projects
MaintainabilityEasier updates

Basic Idea of Encapsulation

Encapsulation means:

Keeping related data and methods together


Example

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

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

Balance and related methods stay together.


Simple Car Example

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

  drive() {
    console.log(`${this.brand} is driving`);
  }
}

const car1 = new Car("BMW", 200);

car1.drive();


Output

BMW is driving


Assignment

Step 1: Create Student Class

class Student {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }

  printDetails() {
    console.log(`${this.name} is ${this.age} years old`);
  }
}


Step 2: Create Objects

const student1 = new Student("Ali", 20);
const student2 = new Student("Sara", 22);


Step 3: Call Method

student1.printDetails();
student2.printDetails();


Output

Ali is 20 years old
Sara is 22 years old


Common Beginner Mistakes

Forgetting new

Incorrect:

const user = Person("Ali", 20);

Correct:

const user = new Person("Ali", 20);


Forgetting this

Incorrect:

name = name;

Correct:

this.name = name;


Key Takeaways

ConceptSummary
OOPOrganizing code using objects
ClassBlueprint
ObjectInstance of class
ConstructorInitializes object
MethodFunction inside class
EncapsulationKeep data and behavior together

Final Notes

OOP is widely used in:

  • Frontend frameworks

  • Backend systems

  • Game development

  • Enterprise applications

Understanding classes and objects is important because they help developers write:

  • Reusable code

  • Structured applications

  • Maintainable systems


0 Comments

Sign in to join the conversation

No comments yet. Be the first to comment!