MDN wants to learn about developers like you:



The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.


arr.indexOf(searchElement[, fromIndex])


Element to locate in the array.

Default: 0 (Entire array is searched)

The index to start the search at. If the index is greater than or equal to the array's length, -1 is returned, which means the array will not be searched. If the provided index value is a negative number, it is taken as the offset from the end of the array. Note: if the provided index is negative, the array is still searched from front to back. If the calculated index is less than 0, then the whole array will be searched.


indexOf compares searchElement to elements of the Array using strict equality (the same method used by the ===, or triple-equals, operator).


Example: Using indexOf

The following example uses indexOf to locate values in an array.

var array = [2, 5, 9];
var index = array.indexOf(2); // index is assigned 0
index = array.indexOf(7); // index is assigned -1

Example: Finding all the occurrences of an element

The following example uses indexOf to find all the indices of an element in a given array, using push to add them to another array as they are found.

var indices = [];
var idx = array.indexOf(element);
while (idx != -1) {
    idx = array.indexOf(element, idx + 1);


indexOf was added to the ECMA-262 standard in the 5th edition; as such it may not be present in all browsers. You can work around this by utilizing the following code at the beginning of your scripts. This will allow you to use indexOf when there is still no native support. This algorithm matches the one specified in ECMA-262, 5th edition, assuming TypeError and Math.abs have their original values.

  if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function (searchElement, fromIndex) {
      if ( this === undefined || this === null ) {
        throw new TypeError( '"this" is null or not defined' );

      var length = this.length >>> 0; // Hack to convert object.length to a UInt32

      fromIndex = +fromIndex || 0;

      if (Math.abs(fromIndex) === Infinity) {
        fromIndex = 0;

      if (fromIndex < 0) {
        fromIndex += length;
        if (fromIndex < 0) {
          fromIndex = 0;

      for (;fromIndex < length; fromIndex++) {
        if (this[fromIndex] === searchElement) {
          return fromIndex;

      return -1;


Specification Status Comment
ECMAScript 5.1 (ECMA-262)
The definition of 'Array.prototype.indexOf' in that specification.
Standard Initial definition.
Implemented in JavaScript 1.6
ECMAScript 6 (ECMA-262)
The definition of 'Array.prototype.indexOf' in that specification.

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes) 1.5 (1.8) 9 (Yes) (Yes)
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support (Yes) (Yes) 1.0 (1.8) (Yes) (Yes) (Yes)

See also

Document Tags and Contributors

 Contributors to this page: Sevenspade
 Last updated by: Sevenspade,