Favorites

JavaScript Array.prototype.indexOf() Method

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
Array.prototype.indexOf()
JavaScript Array Instance methodThis method is supported by all modern browsers.
Updated 28th November 2026

The indexOf() method returns the first index at which a value is found in an array, or -1 if it isn't there.

Description

The indexOf() method returns the first index at which a value appears, using strict equality (===), or -1 if it isn't present. Because of the strict comparison, it can't find NaN.

If you only need to know whether the value is there, includes() is clearer. To search with a condition rather than an exact value, use findIndex().

Its main advantage over includes() is that it tells you where the value is, so you can then remove it with splice(index, 1) or replace it. Remember to check for -1 before using the result as an index. The fromIndex argument lets you continue searching after a previous match to find every occurrence.

Syntax

array.indexOf(searchElement)
array.indexOf(searchElement, fromIndex)

Parameters

  • searchElementThe value to look for.
  • fromIndexOptional. The index to start searching from.

Return Value

The first index of the value, or -1 if it isn't found.

Example

const letters = ['a', 'b', 'c', 'b'];
console.log(letters.indexOf('b'));
console.log(letters.indexOf('b', 2));
console.log(letters.indexOf('z'));

Browser Support

The following information will show you the current browser support for the Array.prototype.indexOf() method. Hover over a browser icon to see the version that first introduced support for this method.

This method is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee