indexOf()

ES5+

Returns the first index at which a given element can be found in the array.

Syntax

array.indexOf(searchElement, fromIndex)

Parameters

searchElement any

Element to locate in the array

Return Value

number

The index of the element, or -1 if not found

Examples

JavaScript
const fruits = ['apple', 'banana', 'cherry'];
console.log(fruits.indexOf('banana'));
console.log(fruits.indexOf('mango'));
Output:
// 1 -1

Related Methods