A fellow dev just asked me how we used to overload JavaScript methods in the old days. Let me introduce you to the arguments array.

JavaScript allows you to pass any arguments a function, even more so than are explicitly listed. The arguments are put into a special object--named arguments.

Take a look at this JavaScript function:

view plain print about
1function myFunction() {
2 console.log(arguments)

No arguments are listed, but we can send in anything we want:

view plain print about
1myFunction(1, 'a', {}, 4)

The method will have access to all arguments, a number 1, the letter a, an empty object, and the number 4.

To do method overloading, you just need to add a lot of conditions to determine what you're doing based on the arguments you have. I'm not a fan of method overloading, especially in JavaScript, because it makes methods complicated.

Play with the code here.