indexOf

Modified on Mon, 07 Aug 2023

Description

Get the index at which the first occurrence of value is found in array, else -1

Syntax

indexOf(array, value, fromIndex)

Parameters

Input ParameterTypeDescription
arrayArrayThe array to inspect
valueanyThe value to search for
fromIndex (optional)numberThe index to search from

Return value

Type
number

Sample

ExampleResultDescription
indexOf([1, 10, 1, 10], 10)1Search for the value 10
indexOf([1, 10, 1, 10], 10, 2)3Search for the value 10 from the index 2
indexOf([1, 2, 3, 4], 10)-1Return -1 if the search value is not found

To view all the List functions, click here.

See Also