indexOf()

ES3+

Returns the index of the first occurrence of a specified value in a string.

Syntax

string.indexOf(searchValue, fromIndex)

Parameters

searchValue string

The value to search for

fromIndex number optional

Position to start searching from

Return Value

number

The index of the first occurrence, or -1 if not found

Examples

JavaScript
const str = 'Hello World';
console.log(str.indexOf('o'));
console.log(str.indexOf('x'));
Output:
// 4 -1

Related Methods