JavaScript (ES2026) reference · String Methods

JavaScript String indexOf() Method

Return the index of the first occurrence of some text, or -1 if it isn't found.

JavaScript (ES2026) Since ES1

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

Syntax

string.indexOf(searchString, position)

Parameters

ParameterDescription
searchString Text to look for.
position optional Index to start from. Default: 0

Return value

An index, or -1.

Examples

Find positionsTry it yourself
const s = "banana";
console.log(s.indexOf("an"), s.indexOf("an", 2), s.indexOf("x"));

Output

1 3 -1
Split around a characterTry it yourself
const email = "[email protected]";
const at = email.indexOf("@");
console.log(email.slice(0, at), email.slice(at + 1));

Output

ada example.com

Version history

VersionChange
JavaScript ES1Added

In other languages

Was this page helpful?