JavaScript Array indexOf() Method

You are Here:

JavaScript Array indexOf() Method

The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <script> var myFruit = ["Apple", "Banana", "Cherry", "Apple"]; document.write("Index of Apple is - " +myFruit.indexOf("Apple") +"<br>"); document.write("Index of Apple is - " +myFruit.indexOf("Apple", 2)); </script> </body> </html>

Syntax

Array.indexOf(value, start)

Parameter Values

ValueTypeExplanation
valueRequiredSpecifies the value to be searched in an array.
startOptionalSpecifies the starting position.
If start is omitted, the default value is 0.

Return Values

ValueExplanation
NumberThe first index at which a given element can be found in the array.
-1If the specified element not found within an array.

Reminder

Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.

We are working to cover every Single Concept in JavaScript.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author