Understanding Object-Oriented Programming in Javascript

Search for a command to run...

No comments yet. Be the first to comment.
This blog series is designed for professionals who want to learn JavaScript step by step. Starting from the basics like variables and data types, we gradually move to functions, DOM manipulation, and real-world mini projects.
Before ES6, JavaScript developers used string concatenation with the + operator const name = "Vishal"; const age = 30; const message = "My name is " + name + " and I am " + age + " years old."; con
What routing means in mobile applications
Most of the react native tutorials teaches you how to build application or new screens but very few talks about the systems. That is why the developers can easily build attractive screens but failed t
As a Javascript developer, you have seen the application become more interactive and started handling more asynchronous operations like API calls, file readings, and timers. Initially, developers rely
While writing a Javascript code, no matter how carefully you have written the code but once the user input a invalid data and the API fails, the application might crash at runtime due to some edge cas

JavaScript gives developers a lot of flexibility, but one thing that confuses almost everyone at first is this. In this article, weβll understand: What this means in JavaScript How this behaves in f

Object-oriented programming is a concept of writing code in which we organize everything around objects.
An object is a combination of
Properties
Methods
Instead of writing separate variables and functions, we bundle them together.
Letβs understand object-oriented programming with a simple example:
Imagine you are a car manufacturer.
You first create a blueprint (design)
π In JavaScript:
Blueprint = Class
Car = Object (Instance)
So instead of rewriting code again and again, we define a class once and reuse it.
A class is a blueprint used to create objects.
It defines:
What properties an object will have
What actions it can perform
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
greet() {
console.log(`Hi, my name is ${this.name}`);
}
}
We use the new keyword to create objects from a class.
const person1 = new Person("Ashish", 25);
const person2 = new Person("Rahul", 30);
person1.greet(); // Hi, my name is Ashish
person2.greet(); // Hi, my name is Rahul
π Each object:
Has its own data
Shares the same structure and methods
The constructor is a special method inside a class.
It runs automatically when an object is created
It initializes object properties
constructor(name, age) {
this.name = name;
this.age = age;
}
π this refers to the current object.
Methods define what an object can do.
greet() {
console.log(`Hi, my name is ${this.name}`);
}
π Methods are shared across all instances (memory efficient).
Encapsulation means:
π Keeping data and methods together and controlling access
Think of a car:
You donβt directly control the engine
You use steering, brakes, accelerator
Same in code:
Internal data is hidden
Access is controlled via methods
class BankAccount {
constructor(balance) {
this.balance = balance;
}
deposit(amount) {
this.balance += amount;
}
getBalance() {
return this.balance;
}
}
π We interact using methods instead of directly modifying data.
OOP helps you write better code:
β Reusable (write once, use many times)
β Organized (structured code)
β Scalable (easy to expand)
β Maintainable (easy to debug and update)
| Concept | Meaning |
|---|---|
| Class | Blueprint |
| Object | Real instance created from class |
Try this yourself π
Create a Student class with:
Properties: name, age
Method: printDetails()
class Student {
constructor(name, age) {
this.name = name;
this.age = age;
}
printDetails() {
console.log(`Name: \({this.name}, Age: \){this.age}`);
}
}
const student1 = new Student("Aman", 20);
const student2 = new Student("Neha", 22);
student1.printDetails();
student2.printDetails();
Class (Blueprint)
β
Create using new
β
Objects (Instances)
Example:
Car (Class)
β
car1, car2, car3 (Objects)
β Forgetting new keyword β Not using this properly β Thinking each object has separate methods (they are shared) β Confusing class with object
OOP is not just a conceptβitβs a way of thinking.
Once you understand:
Class
Object
Constructor
Methods
You can build real-world applications much more easily π
Happy Coding! π»