Javatpoint Logo
Javatpoint Logo

jQuery inArray() method

The jQuery inArray() method is used to find a specific value in the given array. If the value found, the method returns the index value, i.e., the position of the item. Otherwise, if the value is not present or not found, the inArray() method returns -1.

This method does not affect the original array. If the item occurs more than once, the inArray() method returns the index value of the first occurrence. The index of the array begins with 0, so if the value is matched with the first item, the inArray() method returns 0.

Syntax

The inArray() method accepts some parameter values that are defined as follows -

val: It is the item to search for.

arr: It is an array through which to search.

index: It is the array index at which to start the search.

Example1

In this example, we are using the inArray() method to search the specified element from the array. Here, there is an array arr including some elements, and we are finding the item "A" from the array arr. Although the item "A" is occurring many times in the array arr, but the method will return the index of the first occurrence.

Test it Now

Output

After the execution of the above code, the output will be -

jQuery inArray() method

After clicking the given button, the output will be -

jQuery inArray() method

Example2

This example is similar to the previous example, except that here we are using the index parameter of the inArray() method along with other arguments. The method will search the element "A" at index value 4 in the given array arr and will return -1. It is because the string "AA" is present at index value 4 of the array arr.

Test it Now

Output

After the execution of the above code, the output will be -

jQuery inArray() method

After clicking the given button, the output will be -

jQuery inArray() method





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA