Cover Image for jQuery inArray() method
99 views

jQuery inArray() method

The jQuery.inArray() method is used to search for a specified value within an array and returns the index at which the value is found. If the value is not present in the array, it returns -1.

Here’s the basic syntax of the inArray() method:

jQuery.inArray(value, array)

Parameters:

  • value: The value to search for in the array.
  • array: The array in which to search for the value.

Return Value:
The inArray() method returns the index of the found value in the array, or -1 if the value is not present.

Example:

var colors = ["red", "blue", "green", "yellow"];

var indexBlue = jQuery.inArray("blue", colors);
console.log(indexBlue); // Output: 1

var indexPurple = jQuery.inArray("purple", colors);
console.log(indexPurple); // Output: -1

In this example, we have an array colors that contains some color names. We use the inArray() method to search for the values “blue” and “purple” in the array. The method returns the index of “blue” (which is 1) and -1 for “purple” (since it is not present in the array).

The jQuery.inArray() method is particularly useful when you want to check if a specific value exists in an array and determine its position within the array. It provides a convenient way to perform array searches in jQuery.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS