JavaScript Array indexOf() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

Array.prototype.indexOf(value) returns the first index where value appears. If it is missing, you get -1. Need only yes/no? Prefer includes(). Need the last match? Use lastIndexOf().

Remember
array.indexOf(value)
array.indexOf(value, fromIndex)

[1, 2, 3, 2].indexOf(2)    → 1
[1, 2, 3, 2].indexOf(9)    → -1
[NaN].indexOf(NaN)         → -1   // use includes for NaN

compares with ===
does not mutate the array
check with !== -1  (index 0 is valid!)

Syntax

Call array.indexOf. Start with this line.

JavaScript
array.indexOf(value, fromIndex = 0)
CallWhat it does
array.indexOf(value, fromIndex = 0)Primary signature.
array.indexOf(...)Call with the arguments your case needs.

Built into JavaScript. Call indexOf(...) on an array instance — no import required.

Library declarations
JavaScript
array.indexOf(value, fromIndex = 0)

Parameters

ParameterDescription
ArrayRequired. Source to search
valueRequired. Value to look for
fromIndexOptional. Start index; negative counts from the end

Return Value

Return ValueDescription
*Index (≥ 0) or -1

JavaScript Examples

Example 1 — Find the First Index

Get the position of a value, or -1 when it is missing.

JavaScript
const numbers = [10, 20, 30, 20, 40];

console.log(numbers.indexOf(20));  // 1
console.log(numbers.indexOf(99));  // -1
console.log(numbers.indexOf(10));  // 0  (valid index!)
Try It Yourself

How It Works

  1. First match only. 20 appears twice; indexOf returns index 1.
  2. Missing value. 99 is not present → -1.
  3. Index 0 is real. 10 is at the start → 0.
  4. Check with !== -1. Never treat the result as a plain boolean.

Example 2 — Start Later with fromIndex

Skip earlier elements to find a later occurrence.

JavaScript
const numbers = [1, 2, 3, 2, 5];

console.log(numbers.indexOf(2));     // 1
console.log(numbers.indexOf(2, 2));  // 3
console.log(numbers.indexOf(2, 4));  // -1
Try It Yourself

How It Works

  1. Default search. Finds the first 2 at index 1.
  2. Start at 2. Skips the first 2; finds the next at 3.
  3. Start at 4. Only 5 remains → -1.
  4. Negative fromIndex. Counts from the end before searching forward.

Example 3 — indexOf vs includes

Use indexOf for the position; use includes for membership (and for NaN).

JavaScript
const values = [1, NaN, 3];

console.log(values.indexOf(1));     // 0
console.log(values.includes(1));    // true

console.log(values.indexOf(NaN));   // -1
console.log(values.includes(NaN));  // true
Try It Yourself

How It Works

  1. Normal values. Both see 1; indexOf also gives index 0.
  2. indexOf misses NaN. Because NaN === NaN is false.
  3. includes finds NaN. SameValueZero treats NaN as equal to itself.
  4. Pick by need. Index → indexOf; yes/no (or NaN) → includes.

Browser & Runtime Support

Array.prototype.indexOf() is part of ES5. It has been available in browsers and Node.js for a long time.

✓ Baseline · ES5

Array.prototype.indexOf()

Supported in all modern browsers, Internet Explorer 9+, and every current Node.js release.

99% Browser support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
Array.indexOf() Excellent

Bottom line: Safe almost everywhere. Prefer includes() for clearer membership checks in modern code, but indexOf() remains the right tool when you need the index.

Frequently Asked Questions

indexOf() returns the index of the first matching element in the array. If the value is not found, it returns -1.
indexOf() returns a number (index or -1). includes() returns true or false. For simple membership checks, includes() is clearer and also finds NaN correctly.
It returns -1. Always compare with !== -1 (or use includes() for a boolean). Do not use if (arr.indexOf(x)) because index 0 is falsy.
Yes. It compares with ===. The number 2 and the string "2" are different. Also, NaN === NaN is false, so indexOf(NaN) is always -1.
fromIndex is optional. It sets where the search starts. Negative values count from the end of the array.
No. indexOf() only reads the array and returns a number. The original array is unchanged.

Next: join()

Combine array elements into one string with a separator.

join() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful