Array methods

Array.prototype.indexOf()

Try the free JavaScript compiler

Finds the first matching present position with strict equality, skipping holes. NaN never matches. Returns -1 when no value matches.

Formal Syntax & Structure

javascript syntaxjavascript
array.indexOf(value, fromIndex)

Parameters & Attributes

NameTypeDescription
valueanyValue to find.
fromIndexNumberStart; default zero.

Return Value

Number

First matching index or -1.

Working Example

Example Snippetjavascript
const a=[2,7,2];console.log(a.indexOf(2),a.indexOf(2,1));
JavaScript Reference