jQuery API

jQuery.inArray()

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).

  • version added: 1.2jQuery.inArray( value, array [, fromIndex] )

    valueThe value to search for.

    arrayAn array through which to search.

    fromIndexThe index of the array at which to begin the search. The default is 0, which will search the whole array.

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), if we're checking for the presence of value within array, we need to check if it's not equal to (or greater than) -1.

Example:

Report the index of some elements in the array.

<!DOCTYPE html>
<html>
<head>
  <style>
  div { color:blue; }
  span { color:red; }
</style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  
<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>
<script>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));
</script>

</body>
</html>

Demo:

Support and Contributions

Need help with jQuery.inArray() or have a question about it? Visit the jQuery Forum or the #jquery channel on irc.freenode.net.

Think you've discovered a jQuery bug related to jQuery.inArray()? Report it to the jQuery core team.

Found a problem with this documentation? Report it to the jQuery API team.

* All fields are required
  • http://profiles.yahoo.com/u/MX5IHEHREJJGGFYBDCQKWMYK5M Bharat

    What is the performance impact if I use this to find out if the objects in one array is contained in other bigger array. Is there any other faster way to do this?
    Thanks,
    Bharat

  • Anonymous

    I think this function name is inappropriate, “inArray” somehow assumes a boolean return value. The documentation doesn’t show the difference between this and indexOf(), why use this?

  • http://www.learningjquery.com/ Karl Swedberg

    Array.indexOf() is not supported in every browser. You would use this if you wanted to ensure cross-browser support. Here is the code from the source: inArray: function( elem, array ) { if ( array.indexOf ) { return array.indexOf( elem ); } for ( var i = 0, length = array.length; i < length; i++ ) { if ( array[ i ] === elem ) { return i; } } return -1; }

  • NoXi79

    I agree with Znarkus. IMO if the function does the same as Array.indexOf why not call it jQuery.indexOf.

  • Anonymous

    this function not return -1 if not found an element in ie7 and opera9/10. In these browsers it returns undefined. You can check it …

  • http://www.learningjquery.com/ Karl Swedberg

    For version consistency, I suppose. If you’d like to discuss it, please use the jQuery Forum at http://forum.jquery.com/.
    Thanks.

  • Anonymous

    It would appear that performance is hampered in IE and possibly in some other browsers.
    I have run a small test with a very small array and it takes up to a second to return a value.

  • Mztester

    var a = [{"id": "1", "name": "okp"}];
    var c = [{"id": "1", "name": "okp"}];
    var b = a[0];
    var d = {“id”: “1″, “name”: “okp”};
    console.log($.inArray(b,a)); 0
    console.log($.inArray(b,c)); -1
    console.log($.inArray(d,a)); -1
    console.log($.inArray(d,c)); -1

    It’s strange, right?

  • http://twitter.com/dhchow Diana Chow

    IE8 also returns undefined for values not found. Might as well use vanilla indexOf for now until this is consistent.

  • Cypher

    No, it's the intended behaviour. Look at:
    var a = {“id”:42}, b = {“id”:42};
    console.log(a == b); // false
    You agree? a differs from b.
    Now:
    var arr = [a];
    console.log($.inArray(a, arr)); // 0
    console.log($.inArray(b, arr)); // -1
    We putted a in arr, not b. So it's not strange. Agree?

    Even if two objects have the same source, they are not the same.

  • jesso

    It may be obvious to some, but there is a difference between numbers and strings! parseInt() can solve the problem.
    Ex:
    var number = 32;
    var arr = [14, 28, 44, "32"];
    if(jQuery.inArray(number,arr) > -1) {
    alert(‘found’);
    }
    // false because “32″ is a string in the array

  • BertrandChorizo

    it does

  • Znarkus

    Should the docs say that it checks type?

  • http://billy.wenge-murphy.com/ Billy Wenge-Murphy

    >Array.indexOf() is not supported in every browser
    Then why not call it indexOf and make it available when the native alternative isn't?

    I expect an “inArray” function to act like PHP in_array

  • Znarkus

    I asked the same thing a while back in the jQuery forum http://forum.jquery.com/topic/inarray

  • Fabrizio

    shouldn't:
    – return array.indexOf( elem );
    be changed with something similar to
    var ret = array.indexOf( elem );
    return (ret==undefined)?-1:ret;

  • Fabrizio Parrella

    I meant to post this under Karl Swedberg message

  • http://www.learningjquery.com/ Karl Swedberg

    Already answered the question above ( http://api.jquery.com/jQuery.inArray/#comment-31436032 ) as did John Resig in reply to Znarkus on the forum.

  • http://www.learningjquery.com/ Karl Swedberg

    Already answered the question above ( http://api.jquery.com/jQuery.i… ) as did John Resig in reply to Znarkus on the forum.

  • Amjad Masad

    You could easily convert the returned value from inArray to boolean, !!~jQuery.inArray(elm, arr)