Overloading pizza with cheese is fun and it makes it quite delicious. In a similar way, you can overload your functions. Function overloading isn’t built into JavaScript but we do have a quick fix for it. Before looking into function overloading for JS, let us check what function overloading is with a programming language that supports it.

C++ supports function overloading.
Example Time:

 

Here, we can see that depending on the type of the variable passed in as the parameter, the output changes. This is possible in C++ as it enables users to create multiple functions with the same name but different types of variables.

JavaScript nither supports multiple functions with the same name (rhymes) nor strict typing. Hence, we need a workaround. To use function overloading in JS, you can follow this. But what is this…


… Will have to figure out!

 

That’s it. That is what JS supports for now. Also, if you have too many types or conditions to check through, you can always rely on your old friend, the switch statement.

Peace Out!


Weird Programmer

I hate people who use LOL in face to face conversations. Also, if I tell you anything else about myself, then I'll have to KILL you~~!

Leave a Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of
%d bloggers like this: