DEV Community

Cover image for Understanding Polymorphism in JavaScript
henry messiah tmt
henry messiah tmt

Posted on

Understanding Polymorphism in JavaScript

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");
Enter fullscreen mode Exit fullscreen mode

### 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());
Enter fullscreen mode Exit fullscreen mode

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();
Enter fullscreen mode Exit fullscreen mode

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();
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (23)

Collapse
 
charles_nwachukwu_5370f00 profile image
Charles Nwachukwu • Edited

Great article 👌
We need to encourage simplified write ups such as this.

Can we also suggest topics for you to write on?

Collapse
 
henry_messiahtmt_099ca84 profile image
henry messiah tmt

Thank you.

Collapse
 
henry_messiahtmt_099ca84 profile image
henry messiah tmt

Yes, that would be fine, here to impact as positively as possible.

Collapse
 
tamsjazz profile image
Tams Berry

Great write up. Keep it up done paediatric technical writer.

Collapse
 
henry_messiahtmt_099ca84 profile image
henry messiah tmt

Thank you very much for reading through.

Collapse
 
ebiweni_lokoja_f8e97af1f5 profile image
EBIWENI LOKOJA

Perfect work......

Collapse
 
henry_messiahtmt_099ca84 profile image
henry messiah tmt

Thank you.

Collapse
 
elekele_izibeyaalex_2bbb profile image
Elekele Izibeya Alex

Great Work man

Collapse
 
henry_messiahtmt_099ca84 profile image
henry messiah tmt

Thanks.

Collapse
 
edward_tarilado_c117c0e73 profile image
Edward Tarilado

Great Article......I now understand how polymorphism works

Collapse
 
henry_messiahtmt_099ca84 profile image
henry messiah tmt

Thanks.

Collapse
 
samuel_ndiomu_a0348a661c4 profile image
Samuel Ndiomu

Polymorphism made easy, thank you

Collapse
 
henry_messiahtmt_099ca84 profile image
henry messiah tmt

Welcome. Well appreciated.

Collapse
 
adika_david_f4882f8904c91 profile image
ADIKA DAVID • Edited

A great piece. You made the concept very easy to understand. Keep it up

Collapse
 
henry_messiahtmt_099ca84 profile image
henry messiah tmt

Thanks.

Collapse
 
ebiweni_lokoja_f8e97af1f5 profile image
EBIWENI LOKOJA

After a careful read through I must confess this is the best work I have read about polymorphism.
Great work

Collapse
 
henry_messiahtmt_099ca84 profile image
henry messiah tmt

Thank you. I am grateful I can make an input.

Collapse
 
meli_henry_173c75d17fa37b profile image
Meli Henry

Thank you so much sire for breaking this down for beginners like me to understand easily. Could you also explain more beginner friendly java script topics?

Collapse
 
henry_messiahtmt_099ca84 profile image
henry messiah tmt

Thank you very much. I will be writing frequently, please follow me and you are sure of getting more content from this page.

Collapse
 
fubara profile image
Samuel fubara

Great article, good read, simple and easy to understand.