Underscore.js Array Methods

Beginner
6 min read
Updated: Nov 2026
21 method guides
Method index

About This Index

This page is the Underscore.js Arrays index for the tutorial track. Each row links to a full guide with syntax, examples, and FAQs.

Learn collection basics on /underscore-js/collection. Use this page when you need an array-specific API such as _.first(), _.compact(), or _.uniq().

Beginner path.

Start with _.first(), then _.compact(), _.uniq(), and _.range()—then browse the rest by category.

How to Call an Array Method

Pass the array to _, or chain with the Underscore wrapper:

Underscore
const nums = [1, 2, 3, 4, 5];

// Functional style
_.first(nums);          // 1
_.first(nums, 2);       // [1, 2]
_.compact([0, 1, false, 2, "", 3]);  // [1, 2, 3]
_.uniq([1, 2, 1, 4, 1, 3]);          // [1, 2, 4, 3]
_.chunk(nums, 2);       // [[1, 2], [3, 4], [5]]
_.range(5);             // [0, 1, 2, 3, 4]

// Chaining
_(nums).rest().first(2).value();  // [2, 3]

Underscore.js Array Methods Table

All array method guides in this Underscore.js tutorial series (21 tutorials).

Getting Started

4 methods

Read the ends of an array — first, last, rest, and initial.

MethodDescriptionTutorial
_.first()Returns the first element, or the first n elements as a new array.Try it
_.initial()Returns everything but the last entry, or all but the last n entries.Try it
_.last()Returns the last element, or the last n elements as a new array.Try it
_.rest()Returns the rest of the elements after the first (alias: _.tail / _.drop).Try it

Clean & Flatten

3 methods

Drop falsy values, flatten nests, and remove listed items.

MethodDescriptionTutorial
_.compact()Returns a copy with all falsy values removed.Try it
_.flatten()Flattens a nested array; pass true for a shallow one-level flatten.Try it
_.without()Returns a copy of the array with listed values removed.Try it

Set Operations

4 methods

Combine arrays with union, intersection, difference, and uniq.

MethodDescriptionTutorial
_.union()Produces the union of arrays — unique values in order of first appearance.Try it
_.intersection()Returns values present in every array you pass.Try it
_.difference()Values from the first array that are not present in the others.Try it
_.uniq()Produces a duplicate-free version of the array (alias: _.unique).Try it

Zip, Chunk & Object

4 methods

Pair rows, split into chunks, or build objects from key lists.

MethodDescriptionTutorial
_.zip()Merges arrays as rows — pairs corresponding indexes together.Try it
_.unzip()The opposite of zip — splits grouped tuples back into arrays.Try it
_.object()Converts arrays of keys (and optional values) into an object.Try it
_.chunk()Splits an array into groups of a given size.Try it

Index & Search

5 methods

Locate values by equality or by a predicate callback.

MethodDescriptionTutorial
_.indexOf()Returns the index of the first matching value, or -1.Try it
_.lastIndexOf()Returns the index of the last matching value, searching backward.Try it
_.sortedIndex()Finds the index where a value should be inserted to keep order.Try it
_.findIndex()Returns the index of the first value that passes a predicate.Try it
_.findLastIndex()Returns the index of the last value that passes a predicate.Try it

Generate

1 method

Build integer sequences with range.

MethodDescriptionTutorial
_.range()Creates a list of integers from start (inclusive) to stop (exclusive).Try it

Frequently Asked Questions

They are Underscore helpers built for arrays: reading ends (_.first, _.last), cleaning (_.compact, _.flatten), set ops (_.union, _.uniq), pairing (_.zip, _.chunk), indexing (_.indexOf, _.findIndex), and generating sequences (_.range).
Collections (_.each, _.map, _.filter) work on arrays and objects. Array methods focus on array-shaped data—ends, uniqueness, zipping, and indexes. Learn collections first if you are new, then open this Arrays index.
No. Helpers like _.compact, _.uniq, _.without, and _.flatten return new arrays. Readers like _.first and _.indexOf return a value or index without changing the input.
Pass the array as the first argument: _.first(list), _.uniq(list), _.chunk(list, 2). You can also chain: _(list).first(2).value().
Start with _.first(), then _.compact(), _.uniq(), and _.range() from the table below. Use Collections when you need map/filter-style work.

Start with _.first()

Open the first-elements guide—syntax, examples, and FAQs.

_.first() 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.

12 people found this page helpful