Polymorphism in JavaScript (original) (raw)

Last Updated : 19 Feb, 2025

Polymorphism is a core concept in object-oriented programming (OOP) that allows objects to be treated as instances of their parent class, but with the ability to take many forms. In simple terms, polymorphism allows you to call the same method on different objects, and each object responds in its own way.

What is Polymorphism in JavaScript?

Polymorphism is one of the 4 pillars of object-oriented programming languages where **poly means **many and **morphism means **transforming one form into another. Polymorphism means the same function with different signatures is called many times. It allows methods to do different things based on the object it is acting upon.

In JavaScript, polymorphism works in two primary ways:

Method Overriding

Method overriding occurs when a subclass provides its own specific implementation of a method that is already defined in its parent class. When you call this method, JavaScript will use the subclass's implementation instead of the parent's, which is a runtime decision.

JavaScript `

class Animal { speak() { console.log("Animal makes a sound"); } }

class Dog extends Animal { speak() { console.log("Dog barks"); } }

class Cat extends Animal { speak() { console.log("Cat meows"); } }

const dog = new Dog(); dog.speak();

const cat = new Cat(); cat.speak();

`

Output

Dog barks Cat meows

**In this example

Method Overloading (Compile-time Polymorphism)

JavaScript does not natively support method overloading, where multiple methods with the same name but different arguments exist in the same scope. However, method overloading can be simulated by checking the number or type of arguments passed to a function, and executing different logic based on them.

JavaScript `

class Calculator { add(a, b) { if (b === undefined) { return a + a; } return a + b; } }

const calc = new Calculator(); console.log(calc.add(2)); console.log(calc.add(2, 3));

`

**In this example

Note- While JavaScript doesn’t natively support method overloading with function signatures (as in Java or C++), we can still achieve similar functionality through manual argument checking.

**Polymorphism with Functions and Objects

It is also possible in JavaScript that we can make functions and objects with polymorphism.

JavaScript `

class A { area(x, y) { console.log(x * y); } } class B extends A { area(a, b) { super.area(a, b); console.log('Class B') } }

let ob = new B(); let output = ob.area(100, 200);

`

Benefits of using Polymorphism

Use Cases of Polymorphism