Skip to main content

Command Palette

Search for a command to run...

Understanding Object-Oriented Programming in JavaScript

Updated
4 min readView as Markdown

As we continue learning programming, we eventually reach a point where managing large and complex code becomes challenging. Writing everything in simple functions and variables can make programs harder to organize and maintain. This is where Object-Oriented Programming (OOP) helps us structure our code in a cleaner and more logical way.

What Object-Oriented Programming (OOP) Means

Object-Oriented Programming (OOP) is a programming approach where we organize code around objects rather than just functions and variables.

An object represents something with properties (data) and methods (actions). By grouping related data and behavior together, OOP helps us write code that is easier to understand, reuse, and maintain.

Instead of thinking only about steps in a program, we start thinking about things (objects) and how they behave.

A Real-World Analogy: Blueprint → Objects

A good way to understand OOP is by thinking about a blueprint for building houses.

A blueprint defines the structure of a house — such as the number of rooms, doors, and windows. However, the blueprint itself is not the actual house.

Using that blueprint, we can build many houses with the same structure.

In programming:

  • The blueprint is called a class

  • The houses built from the blueprint are called objects

Each object follows the structure defined by the class but can contain different values.

What Is a Class in JavaScript

A class is a template or blueprint used to create objects. It defines the properties and methods that the objects created from it will have.

In JavaScript, we define a class using the class keyword.

Example:

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

  startEngine() {
    console.log(this.brand + " engine started");
  }
}

In this example, the Car class defines:

  • Properties: brand and model

  • Method: startEngine()

Creating Objects Using Classes

Once a class is defined, we can create objects from it using the new keyword.

Example:

let car1 = new Car("Toyota", "Corolla");
let car2 = new Car("Honda", "Civic");

Here:

  • car1 and car2 are objects

  • Both are created from the same Car class

  • Each object has its own data

We can then use the methods defined in the class:

car1.startEngine();
car2.startEngine();

The Constructor Method

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

It is mainly used to initialize properties for the object.

Example:

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

When we create an object like this:

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

The constructor assigns:

  • "Toyota" to brand

  • "Corolla" to model

This allows each object to store its own data.

Methods Inside a Class

Methods are functions defined inside a class that describe what an object can do.

Example:

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

  startEngine() {
    console.log(this.brand + " engine started");
  }

  stopEngine() {
    console.log(this.brand + " engine stopped");
  }
}

Now every object created from this class can perform these actions:

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

car1.startEngine();
car1.stopEngine();

Methods help us define the behavior of objects.

The Basic Idea of Encapsulation

Encapsulation is the concept of bundling data and methods together inside a class while controlling how that data is accessed or modified.

Instead of letting every part of a program directly change important data, we keep it protected inside the object and interact with it through methods.

This helps us:

  • Keep code organized

  • Protect important data

  • Reduce unexpected errors

For example, instead of directly changing a value, we might use a method to update it safely.

Encapsulation is one of the core principles of Object-Oriented Programming and plays an important role in building reliable software.

Conclusion

Object-Oriented Programming helps us structure programs in a more organized and scalable way. By using classes as blueprints, we can create multiple objects that share the same structure and behavior. Concepts like constructors, methods, and encapsulation allow us to write cleaner and more maintainable code. As we continue exploring JavaScript, mastering OOP will help us build more complex and powerful applications.