Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
Polymorphism in JavaScript
Next article icon

Polymorphism in JavaScript

Last Updated : 19 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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: A child class overrides a method of its parent class.
  • Method Overloading (simulated): A function behaves differently based on the number or type of its arguments.

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

  • Dog and Cat override the speak() method defined in the Animal class.
  • Even though all of them are instances of Animal, each class provides its own behavior for the speak() method.
  • This is an example of runtime polymorphism because JavaScript determines at runtime which method to call based on the object's type.

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)); 

Output
4 5 

In this example

  • The add() method behaves differently based on the number of arguments provided.
  • If only one argument is passed, it doubles the value; if two arguments are passed, it adds them.

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); 

Output
20000 Class B 

Benefits of using Polymorphism

  • Code reusability: Polymorphism allows us to reuse the code. This saves the time of the developers from writing the code again and again.
  • Easy Integration and Extension: Polymorphism allows us to extend the functionality of code and add the new classes easily.
  • Simplified Code Maintenance: Polymorphism allows us to change the code when new objects are added in the system. We can add new subclasses and the existing polymorphic methods will continue to work without modification.

Use Cases of Polymorphism

  • UI Components: Different types of UI elements like buttons, text fields, and checkboxes can have unique behaviors but have common interfaces.
  • Database Operations: Methods for different database types can share a common interface while implementing specific logic.
  • File Handling: Implementing the different behaviors but using the same method for reading, writing, and parsing different file formats.
  • API Responses: Function can be used for handling the different types of API responses.

Next Article
Polymorphism in JavaScript

N

neeraj3304
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • javascript-oop
  • JavaScript-Questions

Similar Reads

    How to use Polyfill in JavaScript ?
    A polyfill in JavaScript is a script that adds modern features to older browsers that do not natively support them. To use it, include the polyfill script in your HTML or install it via a package manager, ensuring compatibility with older environments.Polyfill and its featuresBroad Compatibility: Po
    3 min read
    7 Loops of JavaScript
    As a programmer, it's crucial to comprehend loops since they give you a means to repeatedly run a block of code. Loops are a fundamental idea in computer programming. Using loops has a number of advantages:Your code will be more effective if you use loops to automate repetitive processes and carry o
    3 min read
    How JavaScript Works?
    JavaScript is a dynamically typed, cross-platform threaded scripting and programming language, used to put functionality and interactivity at the client side as well as to write logic on the server side of a website. It can display content updates, interactive maps, control multimedia, interactive f
    13 min read
    What is JavaScript?
    JavaScript is a powerful and flexible programming language for the web that is widely used to make websites interactive and dynamic. JavaScript can also able to change or update HTML and CSS dynamically. JavaScript can also run on servers using tools like Node.js, allowing developers to build entire
    6 min read
    JavaScript Online Compiler
    The JavaScript (JS) Online Compiler and Editor is a free online tool providing an integrated development environment (IDE) for creating and executing JavaScript code. Whether you are a beginner looking to learn JavaScript or an experienced developer want to test snippets of code quickly, an online c
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences