Skip to main content

Command Palette

Search for a command to run...

Understanding Object-Oriented Programming in JavaScript

Updated
•5 min read•View as Markdown

As programs grow bigger, managing code becomes harder.
To organize code better, JavaScript supports Object-Oriented Programming (OOP).

OOP helps us structure code using objects and classes, making programs easier to reuse and maintain.

In this guide we will learn:

  • What OOP means

  • Real-world analogy (blueprint → objects)

  • What a class is

  • Creating objects from classes

  • Constructor method

  • Methods inside a class

  • Basic idea of encapsulation


1. What is Object-Oriented Programming (OOP)?

Object-Oriented Programming is a way of writing code using objects that represent real-world things.

Examples:

Real World Program
Car Car object
Student Student object
Bank Account Account object

Each object contains:

Properties → information
Methods → actions

Example:

Student
Name → Pankaj
Age → 22

Action → study()

Real-Life Analogy (Blueprint → Objects)

Imagine a car blueprint.

The blueprint defines:

  • engine

  • color

  • speed

But the blueprint itself is not a car.

When we build cars from that blueprint, we get objects.

Blueprint → Car Class

Car 1 → Red Car
Car 2 → Blue Car
Car 3 → Black Car

In programming:

Class → blueprint
Object → real instance

Blueprint → Object Diagram

       Car Class
        ↓
  ----------------
  | color        |
  | speed        |
  | start()      |
  ----------------
      ↓    ↓    ↓
   Car1  Car2  Car3

2. What is a Class in JavaScript?

A class is a blueprint used to create objects.

Example syntax:

class ClassName {
  constructor() {
  }

  methodName() {
  }
}

3. Creating a Class

Example: Person

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

  greet() {
    console.log("Hello my name is " + this.name);
  }
}

Explanation:

Part Meaning
class Person blueprint
constructor initializes values
greet() method

4. Creating Objects from a Class

To create objects we use the new keyword.

Example:

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

Now we have two objects.


Calling a Method

person1.greet();
person2.greet();

Output

Hello my name is Pankaj
Hello my name is Rahul

Class → Instance Relationship

Class: Person
   ↓
new Person()

   ↓
Object Created

person1
person2
person3

5. Constructor Method

The constructor is a special method that runs automatically when a new object is created.

Example:

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

Creating objects:

let car1 = new Car("BMW", 200);
let car2 = new Car("Audi", 220);

Now:

car1.brand → BMW
car2.brand → Audi

6. Methods Inside a Class

Methods are functions defined inside a class.

Example:

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

  drive() {
    console.log(this.brand + " is driving");
  }
}

Usage:

let car = new Car("BMW", 200);

car.drive();

Output

BMW is driving

7. Encapsulation

Encapsulation means keeping data and behavior together inside a class.

Example:

Car object

Properties
brand
speed

Methods
drive()
stop()

Instead of managing these separately, they stay organized inside the class.

Benefits:

  • cleaner code

  • reusable structure

  • easier maintenance


Example (Student Class)

class Student {

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

  showDetails(){
    console.log("Name: " + this.name);
    console.log("Age: " + this.age);
  }

}

Create objects:

let student1 = new Student("Pankaj", 22);
let student2 = new Student("Rahul", 21);

Use the method:

student1.showDetails();
student2.showDetails();

Output

Name: Pankaj
Age: 22

Name: Rahul
Age: 21

Create a Class

Create a class called Student.

Properties:

  • name

  • age


Add a Method

Add a method that prints student details.

Example idea:

printDetails(){
  console.log(this.name + " is " + this.age + " years old");
}

Create Multiple Objects

Example:

let s1 = new Student("Amit", 20);
let s2 = new Student("Riya", 21);
let s3 = new Student("Rahul", 22);

Call the Method

s1.printDetails();
s2.printDetails();
s3.printDetails();

Final Thought:

Object-Oriented Programming helps you build large and scalable applications.

You will use OOP heavily in:

  • JavaScript applications

  • Node.js backend

  • React applications

  • game development

  • real-world software systems

Mastering classes and objects is a big step toward becoming a strong JavaScript developer.