JavaScript Array indexOf() Method | jquery indexof
ExampleSearchanarrayfortheitem"Apple":constfruits=["Banana","Orange","Apple","Mango"];fruits.indexOf("Apple") //Returns2TryitYourself»[1]More"TryitYourself"examplesbelow.DefinitionandUsageTheindexOf()methodsearchesanarrayforaspecifieditemandreturnsitsposition.Thesearchwillstartatthespecifiedposition(at0ifnostartpositionisspecified),andendthesearchattheendofthearray.indexOf()returns-1iftheitemisnotfound.Iftheitemispresentmorethanonce,theindexOfmethodreturnsthepositionofthefirstoccurence.Not...
ExampleSearch an array for the item "Apple":
const fruits = ["Banana", "Orange", "Apple", "Mango"];fruits.indexOf("Apple") // Returns 2
Try it Yourself »[1]More "Try it Yourself" examples below.
Definition and UsageThe indexOf() method searches an array for a specified item and returns its position.
The search will start at the specified position (at 0 if no start position is specified), and end the search at the end of the array.
indexOf() returns -1 if the item is not found.
If the item is present more than once, the indexOf method returns the position of the first occurence.
Note: The first item has position 0, the second item has position 1, and so on.
Tip: If you want to search from end to start, use lastIndexOf()[2]
Browser SupportindexOf() is fully supported in all modern browsers:
Chrome IE Edge Firefox Safari Opera Yes ...