Underscore.js _.restArguments() Method

Beginner
5 min read
Updated: Nov 2026
4 examples
Function

Definition

Returns a version of a function that, when called, receives all trailing arguments beyond startIndex packed into an array (official Underscore docs). Stable Function helper — not deprecated.

Pass the required arguments shown below. Underscore function helpers usually return a new function — check timing and call-count behavior carefully.

Syntax

Call _.restArguments. Start with this line.

Underscore
_.restArguments(fn, [startIndex])
CallWhat it does
_.restArguments(fn, [startIndex])Primary signature.
_.restArguments(...)Call with the arguments your case needs.

Load Underscore with import _ from "underscore" (or the UMD CDN build) so these helpers are available as _.

Library declarations
Underscore
_.restArguments(fn, [startIndex])

Parameters

ParameterDescription
functionRequired. Function to wrap
startIndexOptional. Where gathering begins (default: length - 1)

Return Value

Return ValueDescription
*The value returned by _.restArguments().

Underscore.js Examples

Example 1 — Race Results

Official docs idea — fixed medals, trailing names as an array.

Underscore
var raceResults = _.restArguments(function (gold, silver, bronze, everyoneElse) {
  return {
    gold: gold,
    silver: silver,
    bronze: bronze,
    rest: everyoneElse
  };
});

raceResults("Dopey", "Grumpy", "Happy", "Sneezy", "Bashful", "Sleepy", "Doc");
// → rest: ["Sneezy","Bashful","Sleepy","Doc"]
Try It Yourself

How It Works

  1. Four params. Default gather starts at index 3.
  2. First three. Stay as gold / silver / bronze.
  3. The rest. Packed into everyoneElse as an array.
  4. Docs use. You could _.each(everyoneElse, …) next.

Example 2 — Gather Messages

One fixed level; any number of message strings.

Underscore
var log = _.restArguments(function (level, messages) {
  return level + ": " + messages.join(", ");
});

log("info", "a", "b", "c");
// → "info: a, b, c"
Try It Yourself

How It Works

  1. Two params. Default startIndex is 1.
  2. First arg. Remains level.
  3. Trailing. Become messages array.
  4. Array API. join works immediately.

Example 3 — Custom startIndex

Keep two leading args; gather from index 2.

Underscore
var take = _.restArguments(function (a, b, rest) {
  return { a: a, b: b, rest: rest };
}, 2);

take(1, 2, 3, 4);
// → { a: 1, b: 2, rest: [3, 4] }
Try It Yourself

How It Works

  1. Override default. Pass 2 as startIndex.
  2. Head. 1 and 2 stay separate.
  3. Tail. 3 and 4 pack into rest.
  4. Control. Use when the last param is not the only gather slot you want.

Example 4 — Empty Rest Is []

No trailing arguments → empty array, not undefined.

Underscore
var tags = _.restArguments(function (name, rest) {
  return name + " has " + rest.length + " tags";
});

tags("post");              // → "post has 0 tags"
tags("post", "js", "web"); // → "post has 2 tags"
Try It Yourself

How It Works

  1. Always an array. Safe to read .length.
  2. Zero extras. Rest is [].
  3. With extras. Length matches trailing count.
  4. No null checks. You can call array methods immediately.

Browser & Runtime Support

_.restArguments ships with Underscore.js (1.x). It runs anywhere Underscore runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install underscore.

✓ Stable · Underscore 1.x

_.restArguments()

Available in [email protected] (UMD / ESM). Works in evergreen browsers and current Node LTS when Underscore is loaded.

100% With Underscore 1.x
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
_.restArguments Underscore 1.x+

Bottom line: Safe for production once Underscore is on the page or in your bundle. Not a native browser API — load Underscore first. Prefer a pinned CDN version (e.g. [email protected]). Native alternative in modern JS: function (a, b, ...rest) {}.

Frequently Asked Questions

_.restArguments(function, [startIndex]) returns a new function that packs all trailing arguments from startIndex into one array and passes that array as the last parameter — like JavaScript rest parameters (...args).
It is where gathering begins. Arguments before it pass through normally. If omitted, it defaults to function.length - 1 so the last declared parameter receives the rest.
Same idea. Modern JS can write function (a, ...rest) {}. _.restArguments builds that behavior at runtime for older environments or existing functions.
Always a real Array (not the arguments object). If there are no trailing args, you get [].
Yes. The wrapper forwards this to the original function.
No. It is a stable Underscore Function helper, and Underscore uses it internally for helpers like _.partial.

Next: Function methods hub

Browse every Underscore.js function helper in one place.

Function methods →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful