Skip to main content

Command Palette

Search for a command to run...

Understanding Object-Oriented Programming in JavaScript

Updated
•6 min read•View as Markdown
Understanding Object-Oriented Programming in JavaScript

As programs grow bigger, organizing code properly becomes very important. Writing everything in random variables and functions can quickly become difficult to manage.

To solve this problem, programming uses a concept called Object-Oriented Programming (OOP).

Object-Oriented Programming helps us structure our code in a way that represents real-world things such as people, cars, students, products, etc.

In this article we will understand:

  • What Object-Oriented Programming means

  • A simple real-world analogy

  • What a class is in JavaScript

  • How to create objects using classes

  • The constructor method

  • Methods inside a class

  • The basic idea of encapsulation

The goal is to understand the core idea of OOP, without going into very advanced topics.


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

Object-Oriented Programming (OOP) is a programming approach where we organize code using objects.

An object represents something with:

  • Properties (data)

  • Methods (actions)

For example, a car can have:

Properties:

  • brand

  • color

  • speed

Methods:

  • start()

  • stop()

  • accelerate()

In programming, we can represent this structure using objects.

Example idea:

Car
 ├─ brand
 ├─ color
 ├─ speed
 ├─ start()
 └─ stop()

OOP helps us model real-world things in code, which makes programs easier to understand and maintain.


2. Real-World Analogy: Blueprint → Objects

To understand OOP better, imagine a car factory.

A factory first creates a blueprint of a car.

The blueprint describes:

  • engine type

  • number of wheels

  • design

  • features

Using the same blueprint, the factory can produce many cars.

Important idea:

Blueprint → Many Cars

In programming:

Class → Many Objects

So:

  • Class = blueprint

  • Object = real instance created from the blueprint

Example:

Class: Car

Objects:
Car1
Car2
Car3

Each object can have different values but follow the same structure.


3. What is a Class in JavaScript?

A class is a template or blueprint used to create objects.

It defines:

  • what properties the object will have

  • what methods the object can perform

JavaScript introduced the class syntax in ES6 to make object-oriented programming easier.

Basic syntax:

class ClassName {
  
}

Example:

class Car {

}

This class does not do anything yet, but it will define the structure of car objects.


4. Creating Objects Using Classes

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

Example:

class Car {

}

let car1 = new Car();
let car2 = new Car();

Here:

  • Car is the class

  • car1 and car2 are objects created from the class

This process is called instantiation.

Class → Object creation → Instance

But currently our objects do not have properties or behavior yet.

So we need something called a constructor.


5. The Constructor Method

A constructor is a special method that runs automatically when an object is created.

It is used to initialize object properties.

Syntax:

constructor(parameters) {

}

Example:

class Car {

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

}

Explanation:

  • constructor() receives values when the object is created

  • this refers to the current object

  • We assign the values to the object's properties

Now we create objects:

let car1 = new Car("Toyota", "Red");
let car2 = new Car("Honda", "Blue");

Visualization:

Car Class
-----------------
brand
color

Objects Created
-----------------
car1 → Toyota, Red
car2 → Honda, Blue

Each object has its own values.


6. Methods Inside a Class

Classes can also contain methods.

Methods are simply functions inside a class that define what the object can do.

Example:

class Car {

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

  displayInfo() {
    console.log(this.brand + " " + this.color);
  }

}

Now create an object:

let car1 = new Car("Toyota", "Red");

car1.displayInfo();

Output:

Toyota Red

Here:

  • displayInfo() is a method

  • this.brand and this.color access object properties

So the class now defines both:

  • data (properties)

  • behavior (methods)


7. Example: Person Class

Let’s create a simple Person class.

class Person {

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

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

}

Create objects:

let person1 = new Person("Rahul", 25);
let person2 = new Person("Anita", 30);

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

Output:

Hello, my name is Rahul
Hello, my name is Anita

Both objects use the same class blueprint.


8. Basic Idea of Encapsulation

Encapsulation means grouping data and methods together inside a single unit (class).

Instead of keeping everything separate, we combine related things.

Example:

Without structure:

let name = "Rahul";
let age = 25;

function greet() {
  console.log("Hello");
}

With encapsulation:

class Person {

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

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

}

Here:

  • data (name, age)

  • behavior (greet())

are grouped inside the Person class.

This makes the code:

  • easier to understand

  • easier to reuse

  • easier to maintain


9. Why OOP is Useful

Object-Oriented Programming helps in several ways.

Code Reusability

We define a class once and create many objects.

Example:

Student class
 → student1
 → student2
 → student3

Better Organization

Instead of scattered variables and functions, everything is structured inside classes.

Easier Maintenance

Large projects become easier to update and manage.


Assignment Practice

Try the following exercise.

1. Create a Student Class

Create a class called Student.

Example:

class Student {

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

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

}

2. Create Multiple Objects

let student1 = new Student("Amit", 20);
let student2 = new Student("Riya", 22);
let student3 = new Student("Arjun", 21);

3. Call the Method

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

Output:

Amit is 20 years old
Riya is 22 years old
Arjun is 21 years old

Summary:

Object-Oriented Programming helps us write structured and reusable code. In JavaScript, we use classes to define the blueprint and create objects from them.

Key ideas to remember:

  • OOP organizes code using objects

  • A class acts as a blueprint

  • Objects are instances created from the class

  • The constructor initializes object properties

  • Methods define behavior

  • Encapsulation groups data and behavior together

Once you become comfortable with these basics, you can explore more advanced OOP concepts like inheritance and polymorphism, which help build even more powerful applications.

Javascript

Part 4 of 12

This series explains core JavaScript concepts in a clear and beginner-friendly way. Each article focuses on one important topic with simple explanations, practical examples, and real development use cases. The goal is to make JavaScript easier to understand for developers who are building their foundation in web development.

Up next

Array Methods You Must Know in JavaScript

Arrays are one of the most used data structures in JavaScript. Most real-world programs deal with lists of data — like a list of users, products, tasks, or marks. JavaScript provides built-in array me