Understanding Object-Oriented Programming in JavaScript

When we build software for some real world use case, the code base becomes large and managing codes become very difficult if everything is written using random variables and functions. That is the very reason why programmers prefer using Object-Oriented Programming .
While building real world entities like bank or Orders in E-commerce system We have to use object oriented programming as each of these entities have data and actions.
For example, a Bank Account has:
Properties (data)
accountHolderName
accountNumber
balance
Actions (methods)
deposit money
withdraw money
check balance
What is Object-Oriented Programming?
Object-Oriented Programming (OOP) is a programming paradigm that organizes code using objects and classes.
An object represents a real-world entity that contains:
Properties → data
Methods → behavior
In our example:
Bank Account Object
Properties
- accountHolderName
- accountNumber
- balance
Methods
- deposit()
- withdraw()
- checkBalance()
Blueprint -> Objects Analogy:
Think about building cars before building cars. Blueprint of a particular model is first created by the architect . Then, using that blueprint, multiple cars of the same model can be built.
Blueprint (Class)
│
▼
BankAccount Object 1
BankAccount Object 2
BankAccount Object 3
In JavaScript:
Class is the Blueprint
Object is the Actual instance created from the class
What is a Class?
A class is a template ( blueprint ) that we use to create objects.
Creating a Bank Account Class
/*
class ClassName {
constructor() {
// properties
}
methodName() {
// behavior
}
}
*/
class BankAccount {//class name
constructor(accountHolderName, accountNumber, balance) {
this.accountHolderName = accountHolderName;
this.accountNumber = accountNumber;
this.balance = balance; //property of the object
}
}
Creating Objects from the Class
We use the new keyword to create instances( objects) from the class.
const account1 = new BankAccount("Sanghita", 101, 5000);
const account2 = new BankAccount("Rahul", 102, 8000);
Now we have two separate bank accounts, based on the same template or class.
Adding Methods to the Class:
Now in the bank account, the user, or the account holder, must have some actions to do, like depositing money, withdrawing money and cheque balance. For doing these actions, we can add some methods.
class BankAccount {
constructor(accountHolderName, accountNumber, balance) {
this.accountHolderName = accountHolderName;
this.accountNumber = accountNumber;
this.balance = balance;
}
deposit(amount) {
this.balance += amount;
console.log("Deposited:", amount);
}
withdraw(amount) {
this.balance -= amount;
console.log("Withdrawn:", amount);
}
checkBalance() {
console.log("Current Balance:", this.balance);
}
}
Now, how the user can use this methods To take Actions?
const account1 = new BankAccount("Sanghita", 101, 5000);
account1.deposit(2000);
account1.withdraw(1000);
account1.checkBalance();
/*
Deposited: 2000
Withdrawn: 1000
Current Balance: 6000
*/
Now What is the Constructor?
Now, so far we have used something called Constructor In the very beginning of every class. Now the question is, this seems to be like a function or method, but we are not calling this method, right?
Let's see what's happening here...
const account1 = new BankAccount("Sanghita", 101, 5000);
When we write this line We are actually calling the constructor. That means, whenever we declare a new object The constructor is automatically being called.
JavaScript automatically assigns:
this.accountHolderName = "Sanghita"
this.accountNumber = 101
this.balance = 5000
What the this keyword refers
this refers to the current object.
Whenever we write this.balance it means balance of the current account object
Encapsulation
Encapsulation means bundling data and methods together inside a class. As the name suggests , you can compare it with capsule that is a medicine.
Example:
class BankAccount {
constructor(name, balance) {
this.name = name;
this.balance = balance;
}
deposit(amount) {
this.balance += amount;
}
}
Here data(balance) & methods(deposit()) both are inside the same class.
Inheritance
Inheritance allows one class to reuse features of another class.
class BankAccount {
constructor(name, balance) {
this.name = name;
this.balance = balance;
}
checkBalance() {
console.log(this.balance);
}
}
//Now create a SavingsAccount class.
class SavingsAccount extends BankAccount {
constructor(name, balance, interestRate) {
super(name, balance);
this.interestRate = interestRate;
}
}
//creating account object
const account = new SavingsAccount("Sanghita", 5000, 5);
account.checkBalance();
The SavingsAccount class inherits features of BankAccount.
Polymorphism (Basic Idea)
Polymorphism means same method behaves differently for different objects.
class BankAccount {
withdraw(amount) {
console.log("Withdraw from bank account:", amount);
}
}
class ATMAccount extends BankAccount {
withdraw(amount) {
console.log("Withdraw from ATM:", amount);
}
}
//Usage
let a = new BankAccount();
let b = new ATMAccount();
a.withdraw(1000);
b.withdraw(1000);
//Output
/*
Withdraw from bank account: 1000
Withdraw from ATM: 1000
*/
Same method → different behavior.
Practice Qs 1
class BankAccount {
constructor(name, balance) {
this.name = name;
this.balance = balance;
}
deposit(amount) {
this.balance += amount;
}
withdraw(amount) {
this.balance -= amount;
}
}
const acc1 = new BankAccount("Sanghita", 5000);
acc1.deposit(2000);
acc1.withdraw(500);
console.log(acc1.balance); //6500





