Your Search Results

    Array.prototype.indexOf()

    Summary

    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.

    Syntax

    arr.indexOf(searchElement[, fromIndex])

    Parameters

    searchElement
    Element to locate in the array.
    fromIndex

    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.

    Description

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

    Examples

    Example: Using indexOf

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

    var array = [2, 5, 9];
    
    console.log(array.indexOf(2));
    // 0
    
    console.log(array.indexOf(7));
    // -1
    
    console.log(array.indexOf(9, 2));
    // 2
    
    console.log(array.indexOf(2, -1));
    // -1
    
    console.log(array.indexOf(2, -3));
    // 0
    
    

    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 array = ["a", "b", "a", "c", "a", "d"];
    var idx = array.indexOf("a");
    while (idx != -1) {
      indices.push(idx);
      idx = array.indexOf("a", idx + 1);
    }
    console.log(indices);
    
    // [0, 2, 4]
    
    

    Polyfill

    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) {
    
        var k;
    
        // 1. Let O be the result of calling ToObject passing
        //    the this value as the argument.
        if (this == null) {
          throw new TypeError('"this" is null or not defined');
        }
    
        var O = Object(this);
    
        // 2. Let lenValue be the result of calling the Get
        //    internal method of O with the argument "length".
        // 3. Let len be ToUint32(lenValue).
        var len = O.length >>> 0;
    
        // 4. If len is 0, return -1.
        if (len === 0) {
          return -1;
        }
    
        // 5. If argument fromIndex was passed let n be
        //    ToInteger(fromIndex); else let n be 0.
        var n = +fromIndex || 0;
    
        if (Math.abs(n) === Infinity) {
          n = 0;
        }
    
        // 6. If n >= len, return -1.
        if (n >= len) {
          return -1;
        }
    
        // 7. If n >= 0, then Let k be n.
        // 8. Else, n<0, Let k be len - abs(n).
        //    If k is less than 0, then let k be 0.
        k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
    
        // 9. Repeat, while k < len
        while (k < len) {
          var kValue;
          // a. Let Pk be ToString(k).
          //   This is implicit for LHS operands of the in operator
          // b. Let kPresent be the result of calling the
          //    HasProperty internal method of O with argument Pk.
          //   This step can be combined with c
          // c. If kPresent is true, then
          //    i.  Let elementK be the result of calling the Get
          //        internal method of O with the argument ToString(k).
          //   ii.  Let same be the result of applying the
          //        Strict Equality Comparison Algorithm to
          //        searchElement and elementK.
          //  iii.  If same is true, return k.
          if (k in O && O[k] === searchElement) {
            return k;
          }
          k++;
        }
        return -1;
      };
    }
    

    Specifications

    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.
    Draft  

    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

    Hide Sidebar