Underscore.js _.toArray() Method

Beginner
5 min read
Updated: Nov 2026
4 examples
Collection

Definition

Creates a real Array from the list (anything that can be iterated over) (official Underscore docs). Useful for transmuting the arguments object. Stable Collection helper — not deprecated.

Pass the required arguments shown below. Underscore collection helpers usually return a new value — check whether the source is mutated.

Syntax

Call _.toArray. Start with this line.

Underscore
_.toArray(list)
CallWhat it does
_.toArray(list)Primary signature.
_.toArray(...)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
_.toArray(list)

Parameters

ParameterDescription
listRequired. Array-like, object, string, or iterable

Return Value

Return ValueDescription
*New real Array (shallow)

Underscore.js Examples

Example 1 — Official: Convert arguments

Turn the arguments object into an array, then slice.

Underscore
(function () {
  return _.toArray(arguments).slice(1);
})(1, 2, 3, 4);
// → [2, 3, 4]
Try It Yourself

How It Works

  1. arguments is array-like, not a real Array.
  2. _.toArray copies values into [1, 2, 3, 4].
  3. .slice(1) drops the first element.
  4. Result: [2, 3, 4] — classic “rest args” pattern.

Example 2 — Object Values

Plain objects become an array of values (keys are dropped).

Underscore
_.toArray({ one: 1, two: 2, three: 3 });
// → [1, 2, 3]

// Same values as:
_.values({ one: 1, two: 2, three: 3 });
// → [1, 2, 3]
Try It Yourself

How It Works

  1. Iterate object values in enumeration order.
  2. Push each value into a new array.
  3. Keys are not kept — only values.
  4. _.values matches for plain objects; _.toArray also covers array-likes.

Example 3 — String & Shallow Array Copy

Strings become character arrays; arrays are shallow-copied.

Underscore
_.toArray("hi");
// → ["h", "i"]

var nums = [1, 2, 3];
var copy = _.toArray(nums);
console.log(copy);           // → [1, 2, 3]
console.log(nums === copy);  // → false
Try It Yourself

How It Works

  1. String: each character becomes an element.
  2. Array: new container, same element references.
  3. nums === copy is false — different arrays.
  4. Nested objects inside are not deep-cloned.

Example 4 — Empty & Nullish

Safe defaults when input may be missing.

Underscore
_.toArray([]);
// → []

_.toArray({});
// → []

_.toArray(null);
// → []

_.toArray(undefined);
// → []
Try It Yourself

How It Works

  1. Empty collections → empty array.
  2. null / undefined → [] (no throw).
  3. Handy guard before chaining array methods.
  4. Need a count? Use _.size.

Browser & Runtime Support

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

_.toArray()

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
_.toArray 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]).

Frequently Asked Questions

_.toArray(list) creates a real Array from anything that can be iterated over. Official example: (function(){ return _.toArray(arguments).slice(1); })(1, 2, 3, 4) → [2, 3, 4].
The arguments object is array-like but not a real array — it lacks many array methods. _.toArray makes a real array you can slice, map, and filter.
For plain objects both return the values as an array. _.toArray also handles arguments, NodeLists, strings, and other array-likes.
No. It is a shallow copy — nested objects stay shared references.
_.toArray(null) and _.toArray(undefined) return an empty array [].
Array.from does similar work in modern engines. _.toArray is the Underscore helper and stays handy inside Underscore chains.

Next: _.size()

Count how many values a collection holds — arrays, objects, and more.

_.size() tutorial →

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