JavaScript (ES2026) reference · Array Methods

JavaScript Array indexOf() Method

Return the first index at which a value is found (using ===), or -1 if it isn't in the array.

JavaScript (ES2026) Since ES5

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

array.indexOf(searchElement, fromIndex)

Parameters

ParameterDescription
searchElement The value to look for.
fromIndex optional Index to start from. Default: 0

Return value

An index, or -1.

Examples

Find a positionTry it yourself
const letters = ["a", "b", "c", "b"];
console.log(letters.indexOf("b"), letters.indexOf("b", 2), letters.indexOf("z"));

Output

1 3 -1
Prefer includes() for yes/no checksTry it yourself
const items = ["x", "y"];
if (items.indexOf("y") !== -1) console.log("found");
if (items.includes("y")) console.log("found (clearer)");

Output

found
found (clearer)

Version history

VersionChange
JavaScript ES5Added

In other languages

Was this page helpful?