Lodash _.head() method
What you’ll learn
- How
_.head(array)reads the first element without mutating the collection. - That
_.firstis an alias and whenundefinedappears for empty input. - How this compares to
array[0], optional chaining, andat(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
undefinedin 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
_.head(array) - array: collection to inspect (array-like values are coerced).
- Returns: the element at index
0, orundefinedwhen the collection is empty or has no slot at0. - Alias:
_.first(array)is identical.
Non-empty array
The first element is returned by reference; mutating it later still affects the original array slot.
import head from "lodash/head";
head(["alpha", "beta", "gamma"]);
// "alpha" Empty array
There is no element at index 0, so Lodash returns undefined. Combine with nullish coalescing when you need a default.
import head from "lodash/head";
head([]);
// undefined
head([]) ?? "fallback";
// "fallback" Array-like object
Lodash treats numeric keys plus length like an array for this read, so the first slot is still index 0.
import head from "lodash/head";
head({ 0: "north", 1: "east", length: 2 });
// "north" 📋 _.head vs [0] vs at(0)
| API | Strength | Note |
|---|---|---|
_.head(array) | Named intent, array-like support | Matches other Lodash imports in a utility module |
array[0] | Zero overhead native syntax | Does not communicate “first element” as loudly in FP pipelines |
array.at(0) | Supports negative indices for other positions | ES2022; for index 0 behaves like [0] on dense arrays |
Pitfalls to avoid
Holes at index 0
A sparse array may still have length > 0 while slot 0 is empty; head returns undefined just like bracket access.
Falsy first elements
0, "", and false are valid first values; do not confuse them with “missing” when branching.
Removing the head
Use _.tail or slice(1) when you need the rest of the list; head never shortens the array.
❓ FAQ
Summary
- Purpose:
_.head(array)returns the first element (index0) orundefinedwhen absent. - Alias:
_.firstis identical; pick the name that reads best in context. - Next: Lodash _.indexOf(), _.fromPairs() (earlier in this track), or the array methods hub.
In Lodash, _.first is simply an alias for _.head; importing either name calls the same implementation.
6 people found this page helpful
