Skip to main content

Command Palette

Search for a command to run...

Object Oriented Programming in JavaScript

Updated
5 min readView as Markdown
Object Oriented Programming in JavaScript

Introduction

As programs grow larger, managing code becomes difficult.

Instead of writing everything as separate functions and variables, developers organize code using a powerful concept called Object-Oriented Programming (OOP).

OOP allows us to structure programs around objects, making code easier to manage, reuse, and understand.

In this article we will learn:

  • What Object-Oriented Programming means

  • What classes are in JavaScript

  • How to create objects using classes

  • What constructors and methods are

  • A basic idea of encapsulation


What is Object-Oriented Programming (OOP)?

Object-Oriented Programming is a programming style where we organize code using objects and classes.

Instead of writing code like this:

let studentName = "Rahul";
let studentAge = 22;
let studentCourse = "Web Development";

We group related data together inside objects.

Example:

const student = {
  name: "Rahul",
  age: 22,
  course: "Web Development"
};

OOP takes this concept further by using classes to create objects.


Real-World Analogy

A good way to understand OOP is using a blueprint analogy.

Think about building cars.

  • A blueprint defines the design of a car.

  • Many cars can be created from the same blueprint.

Real World Programming
Car Blueprint Class
Actual Car Object


What is a Class in JavaScript?

A class is a blueprint used to create objects.

Example:

class Student {

}

This class currently does nothing.

We add properties and methods to make it useful.


Creating Objects Using Classes

Let’s create a Student class.

class Student {

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

}

Now we can create objects using this class.

const s1 = new Student("Rahul", 19);
const s2 = new Student("Amit", 21);

Each object now has its own data.

console.log(s1.name);
console.log(s2.name);

Output:

Rahul
Amit


Constructor Method

The constructor is a special method inside a class.

It runs automatically when we create an object.

Example:

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

Explanation:

  • name and age are parameters

  • this.name assigns values to the object

So when we run:

new Student("Rahul", 22);

The constructor automatically sets:

name = Rahul
age = 22

Methods Inside a Class

Classes can also contain methods.

Methods define behavior of objects.

Example:

class Student {

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

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

}

Now we can call the method.

const s1 = new Student("Rahul", 22);

s1.printDetails();

Output:

Rahul is 22 years old

Methods allow objects to perform actions.


Encapsulation

Encapsulation means bundling data and behavior together.

In our example:

class Student {

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

  printDetails() {
    console.log(this.name, this.age);
  }

}

Here:

  • Data → name, age

  • Behavior → printDetails()

Both are inside the Student class.

This makes code more organized and easier to manage.


Why OOP is Useful

Object-Oriented Programming provides several benefits.

1) Code Reusability

We can create many objects from the same class.

Example:

const s1 = new Student("Rahul", 22);
const s2 = new Student("Amit", 21);
const s3 = new Student("Priya", 23);

No need to rewrite code again.


2) Better Code Organization

Classes keep related code together.

Example:

Student
 ├ name
 ├ age
 └ printDetails()

This structure makes programs easier to understand.


3) Real-World Modeling

OOP allows programs to represent real-world entities.

Examples:

Real World Programming Object
User User class
Car Car class
Product Product class

Assignment Practice

Try implementing this yourself.

Step 1: Create a 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 Multiple Objects

const s1 = new Student("Rahul", 22);
const s2 = new Student("Amit", 21);

Step 3: Call the Method

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

Expected Output:

Rahul is 22 years old
Amit is 21 years old

Conclusion

Object-Oriented Programming helps organize programs using classes and objects.

In this article we learned:

  • What OOP means

  • Real-world blueprint analogy

  • What classes are in JavaScript

  • How to create objects using classes

  • Constructor methods

  • Methods inside a class

  • Basic idea of encapsulation

These concepts make JavaScript programs more structured, reusable, and easier to maintain.