Skip to content

lastIndexOf

lastIndexOf(haystack, needle) → number · String

Find the position of the LAST occurrence of a value. Returns -1 if not found.

  • haystack (required): string or array to search in

  • needle (required): value to find

utlx
lastIndexOf("abcabc", "bc")              // 4 (last occurrence, not first at 1)
lastIndexOf(["A", "B", "A", "C"], "A")   // 2 (last A)
lastIndexOf("hello", "xyz")              // -1

Also: findIndex(array, predicate) — find by condition instead of value. findLastIndex(array, predicate) — last match by condition.

Released under AGPL-3.0.