Search⌘ K

The Power and Perils of arguments

Learn why the arguments object was needed in JavaScript and get to know its pros and cons.

The ability to pass a variable number of arguments to a function is esoteric in many languages, but it is commonplace in JavaScript.

📝Note: JavaScript functions always take a variable number of arguments, even if we define named parameters in function definitions.

Here’s a max() function that takes two named parameters:

Javascript (babel-node)
'use strict';
//START:CODE
const max = function(a, b) {
if (a > b) {
return a;
}
return b;
};
console.log(max(1, 3));
console.log(max(4, 2));
console.log(max(2, 7, 1));
//END:CODE

🙋‍♀️ Emma has a question: We can invoke the function with two arguments, but what if we call it with three arguments?

Most languages ...