liveBook · Manning

Arguments

As explained in Secrets of the JavaScript Ninja, Second Edition

An array-like object available inside non-arrow JavaScript functions that exposes the arguments passed to the current invocation.

Inside a function, `arguments` exposes the values passed to that invocation—even when more values are passed than the function declares parameters. It resembles an array, but it is not an array, so array methods such as `sort` are unavailable. section 4.1.1

Use it with care:

  • `arguments.length` gives the total number of values passed, and indexed access can retrieve extra arguments that have no matching named parameter. section 4.1.1
  • In nonstrict mode, indexed values alias the corresponding named parameters: changing one changes the other. Strict mode prevents that aliasing. section 4.4
  • The object can support variadic functions and function-overloading patterns, but rest parameters reduce the need for it. section 4.1.1
Listing 4.1. Using the arguments parameter
function whatever(a, b, c){                     #A
  
  assert(a === 1, 'The value of a is 1');       #B
  assert(b === 2, 'The value of b is 2');       #B
  assert(c === 3, 'The value of c is 3');       #B

  assert(arguments.length === 5,                #C
    'We’ve passed in 5 parameters');            #C

  assert(arguments[0] === a,                    #D
    'The first argument is assigned to a');     #D
  assert(arguments[1] === b,                    #D
    'The second argument is assigned to b');    #D
  assert(arguments[2] === c,                    #D
    'The third argument is assigned to c');     #D

  assert(arguments[3] === 4,                    #E
    'We can access the fourth argument');       #E
  assert(arguments[4] === 5,                    #E
    'We can access the fifth argument');        #E
}

whatever(1,2,3,4,5);                            #F

A function declares three parameters but receives five values; the example compares named parameters with `arguments.length` and indexed access to the extra values. section 4.1.1

How Secrets of the JavaScript Ninja, Second Edition covers it

How deep it goes
Defines its length and indexed access, explains parameter aliasing and strict-mode behavior, and warns that it is not an array.
Examples
Demonstrates extra arguments beyond declared parameters, summing a variable number of values, and parameter aliasing.
What it assumes
Assumes familiarity with function parameters and call arguments, and contrasts `arguments` with rest parameters.
What you can do after
Apply `arguments.length` and indexed access to inspect excess arguments and implement variable-arity functions.
How it connects
Connects `arguments` to function parameters, rest parameters, variadic functions, function overloading, and strict mode.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Secrets of the JavaScript Ninja, Second Edition. The section links point to the parts of the book it draws on.