Lodash _.includes() method
What you’ll learn
- How
_.includes(collection, value, [fromIndex])answers presence questions across collection types. - SameValueZero perks on arrays (
NaN) versus strict equality pitfalls elsewhere. - When
indexOf,some, or nativeArray.prototype.includesmight win. - Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Skim _.filter() or _.find() when you need every match—not just a yes/no.
- You know arrays store ordered elements and plain objects map keys to values.
- You can open Try-it labs or run snippets locally.
Overview
_.includes is the universal presence probe—arrays, strings-as-text, or object value bags—without writing bespoke loops each time.
Boolean clarity
Returns true/false instead of sentinel indexes.
NaN friendly
Array scans honor SameValueZero so NaN finds itself.
Objects too
Plain-object collections scan enumerable values.
Syntax
_.includes(collection, value, [fromIndex]) - collection: array-like list, string, or plain object Lodash can inspect.
- value: needle to locate—interpretation depends on collection type.
- fromIndex (optional): starting position for array-like searches.
- Returns: boolean.
Array membership
The everyday case—does this primitive appear anywhere in the list?
import includes from "lodash/includes";
[ includes([1, 2, 3], 2), includes([1, 2, 3], 9) ];
// → [true, false] Search plain-object values
Keys are ignored—Lodash walks values and compares against your needle.
import includes from "lodash/includes";
includes({ sku: "A1", name: "bolt" }, "bolt");
// → true Array scan with fromIndex
Skip early duplicates—start searching after the first occurrence.
import includes from "lodash/includes";
includes([10, 20, 30, 10], 10, 1);
// → true (matches the trailing 10) 📋 _.includes vs indexOf, some, native includes
| API | Returns | Best when |
|---|---|---|
_.includes(collection, value) | boolean | Uniform checks across objects, arrays, strings |
_.indexOf(array, value) | index or -1 | You need the position, not just truthiness |
_.some(collection, predicate) | boolean | Matching logic is a predicate function |
Array.prototype.includes | boolean | Pure arrays and tree-shaking without Lodash |
Pitfalls to avoid
Keys versus values
Remember Lodash inspects values—probe keys with has, keyBy, or explicit lookups.
Substring semantics
String collections search substrings; do not expect per-character array behavior unless you pass an array of characters.
Huge collections
Linear scans add up—sorted data might deserve binary helpers from the array toolkit instead.
❓ FAQ
Summary
- Purpose:
_.includes(collection, value, [fromIndex])reports membership with collection-aware rules. - Contrast: reach for
somewhen matching requires predicates beyond equality. - Next: Lodash _.invokeMap(), Lodash _.groupBy() (previous), or collection hub.
For arrays, _.includes uses SameValueZero equality—so NaN can match NaN like modern Array.prototype.includes. For plain objects, Lodash checks values, not keys.
6 people found this page helpful
