What are the best practices for function overloading in JavaScript?

JavascriptWeb DevelopmentFront End Technology

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.

Example

Here’s an example −

function display(a, b, value) {
}
display(30, 15, {"method":"subtract"});
display(70, 90, {"test":"equals", "val":"cost"});
raja
Updated on 16-Jun-2020 11:42:02

Advertisements