jQuery.inArray( value, array [, fromIndex ] )Returns: Number

Description: Search for a specified value within an array and return its index (or -1 if not found).

The $.inArray() method is similar to JavaScript's native .indexOf() method in that it returns -1 when it doesn't find a match. If the first element within the array matches value, $.inArray() returns 0.

Because JavaScript treats 0 as loosely equal to false (i.e. 0 == false, but 0 !== false), to check for the presence of value within array, you need to check if it's not equal to (or greater than) -1.

The comparison between values is strict. The following will return -1 (not found) because a number is being searched in an array of strings:

$.inArray( 5 + 5, [ "8", "9", "10", 10 + "" ] );


Report the index of some elements in the array.

<!doctype html>
<html lang="en">
  <meta charset="utf-8">
  <title>jQuery.inArray demo</title>
  div {
    color: blue;
  span {
    color: red;
  <script src="https://code.jquery.com/jquery-3.5.0.js"></script>
<div>"John" found at <span></span></div>
<div>4 found at <span></span></div>
<div>"Karl" not found, so <span></span></div>
<div>"Pete" is in the array, but not at or after index 2, so <span></span></div>
var arr = [ 4, "Pete", 8, "John" ];
var $spans = $( "span" );
$spans.eq( 0 ).text( jQuery.inArray( "John", arr ) );
$spans.eq( 1 ).text( jQuery.inArray( 4, arr ) );
$spans.eq( 2 ).text( jQuery.inArray( "Karl", arr ) );
$spans.eq( 3 ).text( jQuery.inArray( "Pete", arr, 2 ) );


© The jQuery Foundation and other contributors
Licensed under the MIT License.