Lodash _.head() method

Beginner
⏱️ 6 min read
📚 Updated: May 2026
🎯 3 Code examples
🚀 3 Try-it labs
Lodash

What you’ll learn

  • How _.head(array) reads the first element without mutating the collection.
  • That _.first is an alias and when undefined appears for empty input.
  • How this compares to array[0], optional chaining, and at(0).
  • Try each example in the editor (?tryit=1, 2, 3) with Lodash from a CDN.

Prerequisites

Optional prior read _.fromPairs(); you should know that array indexes start at 0.

  • You understand that reading a missing index yields undefined in JavaScript.
  • You can run snippets in Node or open the Try-it labs in a browser.

Overview

_.head is the Lodash name for “give me the first value.” It reads position 0 on arrays and array-like objects, returns undefined when there is no first element, and never allocates a new collection. Symmetric helpers such as _.last cover the opposite end.

Zero-cost read

No slicing, popping, or shifting—just a single indexed lookup wrapped in a named function.

Array-like friendly

Arguments objects and tagged length shapes work the same way as dense arrays.

Alias first

Use whichever identifier reads clearer inside pipelines or FP-style utilities.

Syntax

javascript
_.head(array)
  • array: collection to inspect (array-like values are coerced).
  • Returns: the element at index 0, or undefined when the collection is empty or has no slot at 0.
  • Alias: _.first(array) is identical.
1

Non-empty array

The first element is returned by reference; mutating it later still affects the original array slot.

javascript
import head from "lodash/head";

head(["alpha", "beta", "gamma"]);
// "alpha"
Try it Yourself
2

Empty array

There is no element at index 0, so Lodash returns undefined. Combine with nullish coalescing when you need a default.

javascript
import head from "lodash/head";

head([]);
// undefined

head([]) ?? "fallback";
// "fallback"
Try it Yourself
3

Array-like object

Lodash treats numeric keys plus length like an array for this read, so the first slot is still index 0.

javascript
import head from "lodash/head";

head({ 0: "north", 1: "east", length: 2 });
// "north"
Try it Yourself

📋 _.head vs [0] vs at(0)

APIStrengthNote
_.head(array)Named intent, array-like supportMatches other Lodash imports in a utility module
array[0]Zero overhead native syntaxDoes not communicate “first element” as loudly in FP pipelines
array.at(0)Supports negative indices for other positionsES2022; for index 0 behaves like [0] on dense arrays

Pitfalls to avoid

Sparse

Holes at index 0

A sparse array may still have length > 0 while slot 0 is empty; head returns undefined just like bracket access.

0

Falsy first elements

0, "", and false are valid first values; do not confuse them with “missing” when branching.

Chain

Removing the head

Use _.tail or slice(1) when you need the rest of the list; head never shortens the array.

❓ FAQ

No. It only reads index 0 (or the resolved first slot for array-like values) and returns that element reference.
It returns undefined. Unlike some APIs, Lodash does not throw for an empty collection.
Yes. first is an alias exported for readability; choose whichever name reads best in your codebase.
For ordinary arrays they agree on the first element. nth adds negative indexing and a default argument shape geared toward arbitrary positions.
Use import head from "lodash/head" or import first from "lodash/first"; both resolve to the same helper.

Summary

  • Purpose: _.head(array) returns the first element (index 0) or undefined when absent.
  • Alias: _.first is identical; pick the name that reads best in context.
  • Next: Lodash _.indexOf(), _.fromPairs() (earlier in this track), or the array methods hub.
Did you know?

In Lodash, _.first is simply an alias for _.head; importing either name calls the same implementation.

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.

6 people found this page helpful