JavaScript indexOf: string and array indexOf in JS (indexof javascript)

Last reviewed: by
JavaScript indexOf: string and array indexOf in JS (indexof javascript)

String.prototype.indexOf and Array.prototype.indexOf both answer “where is the first match?”—they return the zero-based index of the first occurrence, or -1 when nothing matches. On strings the search is case-sensitive; on arrays the match uses strict equality (===). When you need the first index that satisfies a custom test rather than a literal value, use JavaScript findIndex instead.

Verified with Node.js v20.18.2: All code samples and commands in this article were executed successfully.


Diagram: the string "hello" with character indexes 0 through 4 under each letter for zero-based indexOf

javascript
const str = "Hello, World!";
console.log(str.indexOf("World"));
text
7

fromIndex on strings

Diagram: fromIndex shifts the starting position so indexOf can find a later occurrence in the same string

The second argument starts the search at that index (values greater than the string length yield -1).

javascript
const str = "Hello, World!";
console.log(str.indexOf("l", 4));
text
10

Scanning for the next occurrence

Advance fromIndex past the previous hit to walk through duplicates.

javascript
const str = "Hello, World!";
const i1 = str.indexOf("o");
const i2 = str.indexOf("o", i1 + 1);
console.log(i1);
console.log(i2);
text
4
8

Missing substring returns -1

javascript
console.log("abc".indexOf("z"));
text
-1

Array indexOf (value search with ===)

Array indexOf compares elements with Strict Equality.

javascript
const arr = [1, 2, 3, 4, 5];
console.log(arr.indexOf(3));
text
2

Array indexOf with fromIndex

javascript
const arr = [1, 2, 3, 4, 5];
console.log(arr.indexOf(3, 2));
text
2

Duplicate values in an array

javascript
const arr = [1, 2, 3, 4, 5, 3];
const i1 = arr.indexOf(3);
const i2 = arr.indexOf(3, i1 + 1);
console.log(i1);
console.log(i2);
text
2
5

NaN is never found with indexOf

Because === does not treat NaN as equal to itself, [NaN].indexOf(NaN) is always -1. Use findIndex(Number.isNaN) or includes with careful handling if you need to locate NaN.

javascript
console.log([NaN].indexOf(NaN));
text
-1

Negative fromIndex on arrays

For arrays, a negative fromIndex is resolved against the length: the starting index is max(array.length + fromIndex, 0).

javascript
console.log([10, 20, 30, 40].indexOf(30, -2));
text
2

Strings also accept a second argument, but values < 0 are clamped to 0 per the string algorithm—do not assume the same offset-from-end behavior you get on arrays.


Collecting every index with a small loop

indexOf only finds the first match. A tight loop advances fromIndex until -1:

javascript
function findAll(a, x) {
  const results = [];
  let pos = 0;
  const len = a.length;
  while (pos < len) {
    pos = a.indexOf(x, pos);
    if (pos === -1) break;
    results.push(pos);
    pos += 1;
  }
  return results;
}

console.log(JSON.stringify(findAll([1, 2, 3, 2, 1], 2)));
text
[1,3]


Summary

Whether you search javascript indexof, indexof javascript, js indexof, or index of javascript, the same pair of methods answers most questions: String.prototype.indexOf for substrings and Array.prototype.indexOf for element identity with SameValueZero, both returning the first match or -1. The usual FAQ is how to walk every duplicate—advance fromIndex past each hit in a loop, or switch strategies when you need predicates instead of exact matches.

People also ask about NaN and indexOf: NaN is never found by equality search, so use findIndex with Number.isNaN when that is the real requirement, and remember negative fromIndex on arrays starts the search near the tail per the spec. For the last occurrence rather than the first, pair this page with JavaScript lastIndexOf.


References

Olorunfemi Akinlua

Boasting over five years of experience in JavaScript, specializing in technical content writing and UX design. With a keen focus on programming languages, he crafts compelling content and designs user-friendly interfaces to enhance digital …

  • JavaScript
  • Web Design