What are the best practices for function overloading in JavaScript?

Function overloading occurs when a function performs different tasks based on a number of arguments passed to it.

The best practice for function overloading with parameters is to not check the types. The code runs slower when the type is checked and it should be avoided. For this, the last parameter to the methods should be an object

Also, do not check the argument length.


Here’s an example −

function display(a, b, value) {
display(30, 15, {"method":"subtract"});
display(70, 90, {"test":"equals", "val":"cost"});

Updated on: 16-Jun-2020


Kickstart Your Career

Get certified by completing the course

Get Started