Lodash _.sortedIndexOf() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Array

Definition

Like _.indexOf, except it performs a binary search on a sorted array. Returns the lowest matching index, or -1 when the value is absent. Part of Lodash Array since 4.0.0 (stable — not deprecated). Warning: the array must already be sorted ascending.

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

Syntax

Call _.sortedIndexOf on an array. Start with this line.

Lodash
_.sortedIndexOf(array, value)
CallWhat it does
_.sortedIndexOf(array, value)Primary signature.
_.sortedIndexOf(array)Call with an array argument.

Prefer import sortedIndexOf from "lodash/sortedIndexOf" when you want a smaller bundle.

Library declarations
Lodash
_.sortedIndexOf(array, value)

Parameters

ParameterDescription
arrayRequired. Sorted ascending collection to search
valueRequired. Value to look up

Return Value

Return ValueDescription
*Lowest matching index, or -1

Lodash Examples

Example 1 — Official: Lowest Match Among Duplicates

Among equal values, return the leftmost index.

Lodash
_.sortedIndexOf([4, 5, 5, 5, 6], 5);
// → 1
Try It Yourself

How It Works

  1. Sorted source. Values ascend: 4, 5, 5, 5, 6.
  2. Search. Binary search looks for 5.
  3. Leftmost. Matches exist at 1, 2, 3 — return 1.
  4. Tip. Use _.sortedLastIndexOf for the rightmost match.

Example 2 — Missing Value Returns -1

Unlike sortedIndex, a miss is not an insert slot.

Lodash
_.sortedIndexOf([4, 5, 5, 5, 6], 3);
// → -1
Try It Yourself

How It Works

  1. Probe. 3 is not in the array.
  2. Confirm miss. Lodash returns -1.
  3. Contrast. _.sortedIndex(arr, 3) would return 0 (insert at front).
  4. Guard. Always check idx !== -1 before indexing.

Example 3 — Match at the Start

The first element is a valid binary-search hit.

Lodash
_.sortedIndexOf([4, 5, 5, 5, 6], 4);
// → 0
Try It Yourself

How It Works

  1. Target. Look up 4.
  2. Hit. It sits at index 0.
  3. Return. Lowest (and only) match is 0.
  4. Use. Same mental model as indexOf, just faster on sorted data.

Example 4 — Match at the End

The last element is found without a full linear scan.

Lodash
_.sortedIndexOf([4, 5, 5, 5, 6], 6);
// → 4
Try It Yourself

How It Works

  1. Target. Look up 6.
  2. Hit. It sits at the final index 4.
  3. Binary path. Search jumps — it does not walk every slot.
  4. Result. Returns 4.

Example 5 — sortedIndexOf vs sortedIndex

Find existing vs compute insert position for a missing value.

Lodash
var arr = [4, 5, 5, 5, 6];

console.log(_.sortedIndexOf(arr, 3)); // -1  (not found)
console.log(_.sortedIndex(arr, 3));   //  0  (insert at front)
Try It Yourself

How It Works

  1. Lookup. sortedIndexOf asks “is it here?” → -1.
  2. Insert. sortedIndex asks “where would it go?” → 0.
  3. Rule. Need membership? Use sortedIndexOf.
  4. Rule. Need a splice slot? Use sortedIndex.

Browser & Runtime Support

_.sortedIndexOf is a Lodash 4.x Array helper (since 4.0.0). It runs anywhere Lodash runs — modern browsers (via CDN or bundle) and Node.js / Bun / Deno with npm install lodash. Native engines have indexOf / findIndex, but not this binary-search variant by name.

✓ Stable · Lodash 4.0.0+

_.sortedIndexOf()

Available in [email protected] full builds and the lodash/sortedIndexOf modular import. Works in evergreen browsers and current Node LTS when Lodash is installed.

100% With Lodash 4.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
_.sortedIndexOf Lodash 4.0.0+

Bottom line: Safe for production once Lodash is on the page or in your bundle. Prefer import sortedIndexOf from "lodash/sortedIndexOf" for smaller builds. Not a native browser API — load Lodash first.

Frequently Asked Questions

_.sortedIndexOf(array, value) is like _.indexOf, but uses a binary search on a sorted ascending array. It returns the lowest index of a matching value, or -1 if the value is not present.
No. It is a read-only lookup. The array stays unchanged.
sortedIndex returns where to insert a value (always a valid insert index). sortedIndexOf returns where the value already is, or -1 if it is missing.
indexOf scans linearly and works on unsorted arrays. sortedIndexOf is faster on large sorted arrays (O(log n)) but requires ascending order.
SameValueZero — same family as indexOf: NaN matches NaN, and +0 matches -0.
Use import sortedIndexOf from "lodash/sortedIndexOf" or require("lodash/sortedIndexOf") so bundlers can tree-shake unused Lodash code. Full build: import _ from "lodash" then _.sortedIndexOf.

Next: _.sortedLastIndex()

Find the highest index where a value can be inserted in sorted order.

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

10 people found this page helpful