This content originally appeared on DEV Community and was authored by henry messiah tmt
Introduction
Polymorphism is one of the hardest concepts for new programmers to understand. The word itself looks complex and can confuse beginners. Many learners struggle because the explanations they find are filled with theory and technical terms. This makes the topic feel harder than it is.
The aim of this document is to explain polymorphism in simple terms. You will see how it works through clear examples from everyday life. The goal is to help you understand what polymorphism means and how you can apply it in JavaScript. Real life comparisons, like a house lamp, phone charger, vehicles, and remote control, will make the concept practical and easy to remember.
What is Polymorphism
Polymorphism means one name with many forms. A single method or function behaves differently depending on the object or context. This is part of object-oriented programming.
Example: House Lamp
You buy one lamp for your house. You use it in the room, parlor, and kitchen. The same lamp behaves differently in each location.
JavaScript Example
turnOn(location) {
if (location === "room") {
console.log("Lamp gives soft light in the room.");
} else if (location === "parlor") {
console.log("Lamp gives bright light in the parlor.");
} else if (location === "kitchen") {
console.log("Lamp gives white light in the kitchen.");
} else {
console.log("Lamp gives default light.");
}
}
}
const lamp = new HouseLamp();
lamp.turnOn("room");
lamp.turnOn("parlor");
lamp.turnOn("kitchen");
### Other Examples
Phone Charger
One charger works for different devices.
class Charger {
charge(device) {
console.log(`Charging ${device}`);
}
}
const charger = new Charger();
charger.charge("phone");
charger.charge("tablet");
charger.charge("power bank");
Vehicles
All vehicles move but each in its own way.
class Vehicle {
move() {
console.log("This vehicle moves.");
}
}
class Car extends Vehicle {
move() {
console.log("The car drives.");
}
}
class Boat extends Vehicle {
move() {
console.log("The boat sails.");
}
}
class Plane extends Vehicle {
move() {
console.log("The plane flies.");
}
}
const vehicles = [new Car(), new Boat(), new Plane()];
vehicles.forEach(v => v.move());
Remote Control
One remote operates different devices.
Switch on TV,
Play music on speaker,
Control a fan.
Why Polymorphism Matters?
Reuse methods in different contexts,
Keep your code flexible,
Make updates easier.
Discriminator Properties in Polymorphism
In JavaScript, polymorphism often needs a way to decide how a method should behave. This is where discriminator properties help. A discriminator property is a special value inside an object that tells the program which form of behavior to use.
Think of it as a label. The label helps the program pick the right action.
Example with House Lamp
class HouseLamp {
constructor(location) {
this.location = location; // discriminator property
}
turnOn() {
if (this.location === "room") {
console.log("Lamp gives soft light in the room.");
} else if (this.location === "parlor") {
console.log("Lamp gives bright light in the parlor.");
} else if (this.location === "kitchen") {
console.log("Lamp gives white light in the kitchen.");
} else {
console.log("Lamp gives default light.");
}
}
}
const roomLamp = new HouseLamp("room");
const parlorLamp = new HouseLamp("parlor");
const kitchenLamp = new HouseLamp("kitchen");
roomLamp.turnOn();
parlorLamp.turnOn();
kitchenLamp.turnOn();
Here, the property location is the discriminator. It changes how the method turnOn behaves.
Example with Vehicles
class Vehicle {
constructor(type) {
this.type = type; // discriminator property
}
move() {
if (this.type === "car") {
console.log("The car drives.");
} else if (this.type === "boat") {
console.log("The boat sails.");
} else if (this.type === "plane") {
console.log("The plane flies.");
} else {
console.log("Unknown vehicle type.");
}
}
}
const car = new Vehicle("car");
const boat = new Vehicle("boat");
const plane = new Vehicle("plane");
car.move();
boat.move();
plane.move();
Here, the property type acts as the discriminator. It decides how the vehicle moves.
Why Discriminator Properties Help?
They give objects a clear identity,
They make code easier to extend,
Add a new type, define its behavior, and the same method works.
They keep your methods reusable but context-aware.
conclusions
Polymorphism lets you use one function or method name in many forms. The context decides the behavior.
A discriminator property is like a switch inside an object. It tells the method how to behave. This makes polymorphism more practical and easier to control in real projects.
This content originally appeared on DEV Community and was authored by henry messiah tmt