Underscore.js _.first() Method

Beginner
5 min read
Updated: Nov 2026
4 examples
Array

Definition

Returns the first element of an array. Passing n returns the first n elements (official Underscore docs). Aliases: _.head, _.take.

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

Syntax

Call _.first. Start with this line.

Underscore
_.first(array, [n])
CallWhat it does
_.first(array, [n])Primary signature.
_.first(...)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
_.first(array, [n])

Parameters

ParameterDescription
arrayRequired. Source to read from the start
nOptional. If set, return first n items as an array

Return Value

Return ValueDescription
*First element, or undefined if empty
*New array of up to n leading items

Underscore.js Examples

Example 1 — Official: First Element

Without a count, _.first returns a single value — the official docs example.

Underscore
_.first([5, 4, 3, 2, 1]);
// → 5
Try It Yourself

How It Works

  1. Source. Five numbers; the front slot is index 0.
  2. No count. Underscore returns that single element, not an array.
  3. Result. 5 — same idea as arr[0].
  4. Safe read. The original array is unchanged.

Example 2 — First n Elements

Pass a count to get a new array of leading items.

Underscore
_.first([5, 4, 3, 2, 1], 2);
// → [5, 4]
Try It Yourself

How It Works

  1. Count. n = 2 means “take two from the start.”
  2. New array. You get [5, 4], not a single number.
  3. Order. Leading order is preserved.
  4. Source. The input list stays [5, 4, 3, 2, 1].

Example 3 — _.head & _.take

Aliases match _.first exactly — useful when “head” or “take” reads clearer.

Underscore
var letters = ["a", "b", "c", "d"];

_.head(letters);       // → "a"
_.take(letters, 2);    // → ["a", "b"]
Try It Yourself

How It Works

  1. Same function. _.head / _.take point at _.first.
  2. One element. _.head(letters) → "a".
  3. With count. _.take(letters, 2) → ["a", "b"].
  4. Style. Use whichever name your team prefers — results match.

Example 4 — Empty Array & Bounds

Empty lists and oversized counts stay predictable — no throws.

Underscore
_.first([]);              // → undefined
_.first([], 3);           // → []
_.first([1, 2, 3], 0);    // → []
_.first([1, 2, 3], 9);    // → [1, 2, 3]
Try It Yourself

How It Works

  1. Empty, no n. Nothing to return → undefined.
  2. Empty with n. Asking for a slice of nothing → [].
  3. Zero count. First 0 items → [].
  4. Oversized n. You get the whole array (no padding).

Browser & Runtime Support

_.first (and aliases _.head / _.take) 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

_.first()

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

_.first(array) returns the first element. Pass a count with _.first(array, n) to get the first n elements as a new array.
_.head and _.take are aliases — same function, same behavior. Pick the name that reads best in your code.
Without n, _.first([]) returns undefined. With n, such as _.first([], 2), it returns [].
No. It only reads. The n-form returns a new array; the source is unchanged.
For one element they match on a non-empty array. _.first also supports the n-form and returns undefined (not an error) for an empty array without n.
_.first reads from the start; _.last reads from the end. Both support an optional n count.

Next: _.initial()

Return everything but the last entry — especially handy on arguments.

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