dexOf() - JavaScript - MDN - Mozilla

  • Skip to main content
  • Skip to search
Array.prototype.indexOf() Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

  • Learn more
  • See full compatibility
  • Report feedback

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

In this article

  • Try it
  • Syntax
  • Description
  • Examples
  • Specifications
  • Browser compatibility
  • See also

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

jsindexOf(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 strict equality (the same algorithm used by the === operator). 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.

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

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

Finding all the occurrences of an element

jsconst 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

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

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

jsconst 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

Was this page helpful to you? Yes No Learn how to contribute

This page was last modified on Jul 10, 2025 by MDN contributors.

View this page on GitHub • Report a problem with this content Filter sidebar
  1. Standard built-in objects
  2. Array
  3. Constructor
    1. Array()
  4. Static methods
    1. from()
    2. fromAsync()
    3. isArray()
    4. of()
  5. Static properties
    1. [Symbol.species]
  6. Instance methods
    1. at()
    2. concat()
    3. copyWithin()
    4. entries()
    5. every()
    6. fill()
    7. filter()
    8. find()
    9. findIndex()
    10. findLast()
    11. findLastIndex()
    12. flat()
    13. flatMap()
    14. forEach()
    15. includes()
    16. indexOf()
    17. join()
    18. keys()
    19. lastIndexOf()
    20. map()
    21. pop()
    22. push()
    23. reduce()
    24. reduceRight()
    25. reverse()
    26. shift()
    27. slice()
    28. some()
    29. sort()
    30. splice()
    31. toLocaleString()
    32. toReversed()
    33. toSorted()
    34. toSpliced()
    35. toString()
    36. unshift()
    37. values()
    38. with()
    39. [Symbol.iterator]()
  7. Instance properties
    1. length
    2. [Symbol.unscopables]
  8. Inheritance
  9. Object/Function
  10. Static methods
    1. apply()
    2. bind()
    3. call()
    4. toString()
    5. [Symbol.hasInstance]()
  11. Static properties
    1. displayName Non-standard
    2. length
    3. name
    4. prototype
    5. arguments Non-standard Deprecated
    6. caller Non-standard Deprecated
  12. Instance methods
    1. __defineGetter__() Deprecated
    2. __defineSetter__() Deprecated
    3. __lookupGetter__() Deprecated
    4. __lookupSetter__() Deprecated
    5. hasOwnProperty()
    6. isPrototypeOf()
    7. propertyIsEnumerable()
    8. toLocaleString()
    9. toString()
    10. valueOf()
  13. Instance properties
    1. __proto__ Deprecated
    2. constructor

Từ khóa » Hàm Indexof