Search⌘ K
AI Features

The Power and Perils of arguments

Explore how JavaScript functions handle variable numbers of arguments through the arguments object and why the rest parameter is a better modern alternative. Understand the limitations of the traditional arguments object, learn how rest parameters improve code clarity and flexibility, and gain practical skills to write more efficient functions.

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 ...