All topics
Functionsbeginner

The arguments Object and Rest Parameters

The array-like arguments object available in regular functions, and the modern rest parameter syntax that replaces most of its use cases.

arguments is an array-like object holding every argument passed to a regular function. Rest parameters replaced nearly every use of it.

`arguments` is like a duffel bag stuffed with everything you brought on a trip, technically holding your stuff but not built with the same compartments and pockets (array methods) real luggage (a proper Array) has. Rest parameters are that proper suitcase — same contents, but organized and ready to use immediately.

Key Concepts

1
    function old() {
      console.log(arguments.length);
      console.log(arguments[0]);
    }
2
    function modern(...args) {
      console.log(args.length);
      console.log(args[0]);
    }
3
Three reasons rest is better.It is a real array. arguments is array-like — it has length and indices and none of the methods:
argumentslength
4
    arguments.map(...)          // TypeError
    Array.from(arguments).map   // the old workaround
    args.map(...)               // just works
5
It is explicit. The signature says the function takes a variable number. arguments is invisible until you read the body.
arguments
6
It can be partial.
7
    function f(first, second, ...rest) { }
    f(1, 2, 3, 4);   // first=1, second=2, rest=[3, 4]
8
arguments always holds everything, so skipping the named parameters means slicing by hand.
arguments
9
Arrows have no arguments at all, which is deliberate — they take it from the enclosing scope, the same as this. Rest parameters are the only option there.
argumentsthis
10
Rest against spread, which use the same three dots in opposite directions:
11
    function f(...args) {}       // rest: collect into an array
    f(...[1, 2, 3]);             // spread: expand into arguments
12
    const [first, ...others] = arr;      // rest, destructuring
    const copy = [...arr, 4];            // spread
    const merged = { ...a, ...b };       // spread, objects
13
Rest gathers on the left of an assignment or in a parameter list. Spread scatters on the right or in a call.
14
Rest must be last. function f(...args, last) is a syntax error, because there would be no way to know where the rest ends.
function f(...args, last)
15
Where arguments still appears. Older codebases and transpiled output. Recognising it matters; writing it does not.
arguments
16
One historical note worth knowing. In sloppy mode, arguments is linked to the named parameters — changing one changes the other. Strict mode removed that, which is one of the reasons strict mode exists.
arguments
17
What the interviewer is probing.1. "Why prefer rest parameters?" Probing: the practical difference. Stalls: "They are newer." Moves up: rest gives a real array, works in arrow functions, and names only the extra arguments — arguments is array-like and needs converting.
18
2. "How do you convert arguments to an array?" Probing: the idioms. Stalls: "It already is one." Moves up: Array.from(arguments) or spread — it has length and indices but no array methods.
19
3. "Is arguments live?" Probing: the legacy behaviour. Stalls: "No." Moves up: in sloppy mode it stays linked to the named parameters, so changing one changes the other; strict mode breaks that link.
20
4. "Where must a rest parameter appear?" Probing: the rule. Stalls: "Anywhere." Moves up: last, because it absorbs everything remaining.