Understand Polymorphism with Example – JavaScript Tutorial

In this tutorial, we will understand polymorphism in detail. We will take a look at polymorphism example and along with that, we going to learn Maps. In the previous article, we had learned Prototype Inheritance. Using the Prototypal Inheritance you can access all the properties and methods of the parent class using child class. To understand the concept behind Prototype Inheritance read this article also.


Polymorphism is a core concept of an Object Oriented paradigm that Provides a way to perform a single action in different forms. This provides an ability to call the same method on different JavaScript objects. Using polymorphism in object-oriented programming you can create Variable methods and objects that have more than one form.

The use of the polymorphism in JavaScript is to override property or method. If you wanted to override any property or method in the parent class then you can do that using the polymorphism.

For example,

So when you execute this statement you will get your message on the console. employee name and his salary.

You can see here we had specified two methods with the same name. So the second child class method will override the parent class method. You can also notice we are accessing both class methods using the child class instance. Now suppose if you wanted to execute the parent class method on the console then just comment this child method or change the name of this method,

so will just comment on this method and execute this again.

You will get your employee method of the parent class. You can override class properties also.

just like this. So here I will just add one more property.

So when you execute this child class property will overwrite the parent class property.  the important thing to note is when you wanted to override any property you need to specify the same name for both properties.

String Representation

Every object ultimately inherits from object. So the methods available for the object are by default available for all objects. One of those methods is toString(). The toString() method provide a default string representation of the object. Having a toString() method that says something descriptive about an object.

For example,

Show the basic use of toString() method is to convert an object into a string type. You can use this method if you want to return any string to the specific variable.

Now let’s move on to the next topic Maps.


ECMAScript 6 a new data structure introduced which is maps. The map is just like an object that They can map a key to a value. When you need to map a key to a value you would turn into an object. Because object allows you to map string keys to object values of any type.

So then why JavaScript need to introduce a new data structure. If you wanted to map a key to a value than the object has some drawback.

  1. The prototypal nature of the object that there could be mappings that you didn’t intend.
  2. As you know in object key must be a symbol or string. This nature preventing you from mapping object to a value.
  3. Also, there is no easy way to know How many mapping there are in the object and you also no object does not guarantee Any order to their properties.

Show the map object address this scarcity. The map of Object hold a key-value pair and remember The origin insertion of the keys. You can use any value as a key or value in the map. You are free to specify any type of values I primitive or object when you are specifying keys or values to the map. To get the size of the map you can use size property.

Now let’s understand how to create maps.

Map() Constructor

When you wanted to create a map you need to understand two methods first.  set and get. Set method is used to set the value for the key in the map object. And get method is used to Return a value associated with the key,  undefined if there is none.

Now let’s take an example and understand map object well.

In the above example, we had create map using set() method and access map key values using get() method. To know map size we used map.size property.

Now let’s understand how to iterate the map object. Using for loop you can iterate map object.

There is another way to declare map object. The map object has a relation with array objects. So you can specify an array in the map object.

Like this.

In the above example, we had created an array in the map object constructor. This will create a map object of size three. To iterate the map object properties we are using forEach() Loop. This loop will print Map keys and Values on the console.

That’s it. I hope you understand this article, we hope you have learned something from this article.