• Skip to main content
  • Skip to search
  • Skip to select language
HTML

Structure of content on the web

  • Web APIs

    Interfaces for building web applications

  • Learn
    • CSS

      Learn to style content using CSS

    • Overview

      A customized MDN experience

    • FAQ

      Frequently asked questions about MDN Plus

  • HTTP Observatory

    Scan a website for free

  • JavaScript
  • indexOf()
      • Deutsch
      • Español
      • Français
      • 日本語
      • 한국어
      • Português (do Brasil)
      • Русский
      • 中文 (简体)
      • 正體中文 (繁體)

    In this article

    • Try it
    • Syntax
    • Description
    • Examples
    • Specifications
    • Browser compatibility
    • See also
    1. from()
    2. [Symbol.species]
  • Instance methods
    1. entries()
    2. find()
    3. flat()
    4. indexOf()
    5. map()
    6. reduceRight()
    7. some()
    8. toReversed()
    9. unshift()
    10. length
    11. bind()
    12. displayName Non-standard
    13. arguments Non-standard Deprecated
    14. caller Non-standard Deprecated
  • Instance methods
    1. __defineGetter__() Deprecated
    2. __defineSetter__() Deprecated
    3. __lookupGetter__() Deprecated
    4. __lookupSetter__() Deprecated
    5. toLocaleString()
    6. __proto__ Deprecated
    7. Array instances returns the first index at which a given element can be found in the array, or -1 if it is not present.

  • Try it

    const beasts = ["ant", "bison", "camel", "duck", "bison"];
    
    console.log(beasts.indexOf("bison"));
    / Expected output: 1
    
    / Start from index 2
    console.log(beasts.indexOf("bison", 2));
    / Expected output: 4
    
    console.log(beasts.indexOf("giraffe"));
    / Expected output: -1
    

    Syntax

    js
    indexOf(searchElement)
    indexOf(searchElement, fromIndex)
    

    Parameters

    searchElement

    Element to locate in the array.

    fromIndex Optional

    Zero-based index at which to start searching, converted to an integer.

    • Negative index counts back from the end of the array — if -array.length <= fromIndex < 0, fromIndex + array.length is used. Note, the array is still searched from front to back in this case.
    • If fromIndex < -array.length or fromIndex is omitted, 0 is used, causing the entire array to be searched.
    • If fromIndex >= array.length, the array is not searched and -1 is returned.

    Return value

    The first index of searchElement in the array; -1 if not found.

    Description

    The indexOf() method compares searchElement to elements of the array using NaN values are never compared as equal, so indexOf() always returns -1 when searchElement is NaN.

    The indexOf() method skips empty slots in sparse arrays.

    The indexOf() method is generic. It only expects the this value to have a length property and integer-keyed properties.

    Examples

    Using indexOf()

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

    js
    const array = [2, 9, 9];
    array.indexOf(2); / 0
    array.indexOf(7); / -1
    array.indexOf(9, 2); / 2
    array.indexOf(2, -1); / -1
    array.indexOf(2, -3); / 0
    

    You cannot use indexOf() to search for NaN.

    js
    const array = [NaN];
    array.indexOf(NaN); / -1
    

    Finding all the occurrences of an element

    js
    const indices = [];
    const array = ["a", "b", "a", "c", "a", "d"];
    const element = "a";
    let idx = array.indexOf(element);
    while (idx !== -1) {
      indices.push(idx);
      idx = array.indexOf(element, idx + 1);
    }
    console.log(indices);
    / [0, 2, 4]
    

    Finding if an element exists in the array or not and updating the array

    js
    function updateVegetablesCollection(veggies, veggie) {
      if (veggies.indexOf(veggie) === -1) {
        veggies.push(veggie);
        console.log(`New veggies collection is: ${veggies}`);
      } else {
        console.log(`${veggie} already exists in the veggies collection.`);
      }
    }
    
    const veggies = ["potato", "tomato", "chillies", "green-pepper"];
    
    updateVegetablesCollection(veggies, "spinach");
    / New veggies collection is: potato,tomato,chillies,green-pepper,spinach
    updateVegetablesCollection(veggies, "spinach");
    / spinach already exists in the veggies collection.
    

    Using indexOf() on sparse arrays

    You cannot use indexOf() to search for empty slots in sparse arrays.

    js
    console.log([1, , 3].indexOf(undefined)); / -1
    

    Calling indexOf() on non-array objects

    The indexOf() method reads the length property of this and then accesses each property whose key is a nonnegative integer less than length.

    js
    const arrayLike = {
      length: 3,
      0: 2,
      1: 3,
      2: 4,
      3: 5, / ignored by indexOf() since length is 3
    };
    console.log(Array.prototype.indexOf.call(arrayLike, 2));
    / 0
    console.log(Array.prototype.indexOf.call(arrayLike, 5));
    / -1
    

    Specifications

    Specification
    ECMAScript® 2026 Language Specification
    # sec-array.prototype.indexof

    Browser compatibility

    See also

    • Polyfill of Array.prototype.indexOf in core-js
    • es-shims polyfill of Array.prototype.indexOf
    • Indexed collections guide
    • Array
    • Array.prototype.findIndex()
    • Array.prototype.findLastIndex()
    • Array.prototype.lastIndexOf()
    • TypedArray.prototype.indexOf()
    • String.prototype.indexOf()

    Help improve MDN

    Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

    Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant