Skip to main content

The arguments keyword

What arguments is

arguments is a built-in object available inside any regular function (but not an arrow function). It contains all the arguments passed to that function on call.


Example

javascript
function showArguments() { console.log(arguments); } showArguments("apple", 42, true);

Result in the console:

javascript
[Arguments] { '0': 'apple', '1': 42, '2': true }

So arguments is an array-like object:

  • indices (0, 1, 2, ...)
  • a length property
  • but no array methods (map, forEach, etc.)

Accessing arguments

javascript
function sumAll() { let total = 0; for (let i = 0; i < arguments.length; i++) { total += arguments[i]; } return total; } console.log(sumAll(1, 2, 3, 4)); // 10

Here we don't declare parameters, we just take everything passed via arguments.


Difference from parameters

javascript
function show(a, b) { console.log(a, b); console.log(arguments[0], arguments[1]); } show("x", "y"); // a,b → "x","y" // arguments[0], arguments[1] → "x","y"

They match as long as you don't change the values.


Changes to arguments and parameters (in regular functions)

javascript
function demo(a) { console.log(a, arguments[0]); // 10, 10 a = 20; console.log(a, arguments[0]); // 20, 20 } demo(10);

In regular functions, arguments and parameters are linked - changing one changes the other (but this does not work in strict mode ('use strict')).


Arrow functions have no arguments

javascript
const arrow = () => { console.log(arguments); // ReferenceError }; arrow(1, 2, 3);

Why:

  • Arrow functions do not create their own arguments object.
  • If you access it, it is taken from the outer function, if there is one.

The alternative - the rest (...) operator

A modern and safe alternative to arguments - rest parameters:

javascript
function sumAll(...args) { return args.reduce((sum, n) => sum + n, 0); } console.log(sumAll(1, 2, 3)); // 6

Advantages of ...args over arguments:

  • It's a real array (has .map, .filter, .reduce).
  • Works in arrow functions.
  • No confusion with "linked" values.
  • The code looks cleaner and clearer.

Quick summary

Propertyarguments...rest
TypeArray-like objectArray
Array methods (map, forEach)NoYes
Available in arrow functionsNoYes
Linked to parametersYes (in non-strict)No
Modern wayDeprecatedRecommended

Example to remember

javascript
// Old way function oldSum() { return Array.from(arguments).reduce((a, b) => a + b); } // New way const newSum = (...nums) => nums.reduce((a, b) => a + b); console.log(oldSum(1, 2, 3)); // 6 console.log(newSum(1, 2, 3)); // 6

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.