jQuery inArray() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Utilities

Definition

jQuery.inArray(value, array[, fromIndex]) (alias $.inArray) searches an array for a value with strict equality (since jQuery 1.2). It returns the index, or -1 if the value is missing.

Syntax

Start with these common calls. Keep them short and readable.

jQuery
$.inArray(value, array)           → index | -1
$.inArray(value, array, fromIndex)

arr = [4, "Pete", 8, "John"]

$.inArray("John", arr)     → 3
$.inArray(4, arr)          → 0
$.inArray("Karl", arr)     → -1
$.inArray("Pete", arr, 2)  → -1
CallWhat it does
inArray()Learn jQuery.inArray(): find a value’s index in an array, handle -1 when missing, use fromIndex, strict equality pitfalls, compare with indexOf and includes, examples, and FAQs.

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Have an array (or array-like list) to search.
Step 2Call $.inArray(value, array).
Step 3If the result is -1, the value was not found.

Return Value

Return ValueDescription
Value / undefinedGetters usually return a value from the first matched element, or undefined when nothing applies.

jQuery Examples

Example 1 — Find Indexes in a Mixed Array

Official pattern: search strings and numbers.

jQuery
var arr = [4, "Pete", 8, "John"];

console.log($.inArray("John", arr));  // 3
console.log($.inArray(4, arr));       // 0
Try It Yourself

How It Works

  1. Array. Mixed values: number, string, number, string.
  2. Find John. "John" sits at index 3.
  3. Find 4. The number 4 is at index 0.

Example 2 — Missing Value and Membership Check

Use !== -1 to ask “is it in the array?”

jQuery
var arr = [4, "Pete", 8, "John"];
var idx = $.inArray("Karl", arr);

console.log(idx);           // -1
console.log(idx !== -1);    // false
Try It Yourself

How It Works

  1. Search. "Karl" is not in the list.
  2. Result. $.inArray returns -1.
  3. Membership. idx !== -1 is the safe boolean test.

Example 3 — Start Later with fromIndex

Skip earlier slots when you only care about the rest of the array.

jQuery
var arr = [4, "Pete", 8, "John"];

console.log($.inArray("Pete", arr));     // 1
console.log($.inArray("Pete", arr, 2));  // -1
Try It Yourself

How It Works

  1. Full scan. From index 0, "Pete" is at 1.
  2. Start at 2. The third argument skips indexes 0 and 1.
  3. Missed. "Pete" is already behind the start point.

Frequently Asked Questions

jQuery.inArray(value, array[, fromIndex]) searches an array for value using strict equality (===). It returns the zero-based index where value is found, or -1 if it is not present.
Same convention as Array.prototype.indexOf: -1 means not found. Check membership with $.inArray(val, arr) !== -1 — never with a plain if ($.inArray(...)), because index 0 is found but falsy.
Both use strict equality and return -1 when missing. indexOf is a native Array method. inArray is a jQuery static utility that also works on array-like objects with length and indexed values.
When provided, the search starts at that index instead of 0. Useful for later occurrences. If the value only appears before fromIndex, the result is -1.
Strict equality (===). The number 10 and the string "10" are different — $.inArray(10, ["8", "9", "10"]) returns -1.
Use $.inArray() when you need the index, or when you already use jQuery with array-like input. Use includes() for a simple true/false membership check on real arrays in modern JavaScript.
Yes, if jQuery is loaded. It is a data utility and does not need a DOM. Many Node projects prefer native indexOf or includes instead.

Next: uniqueSort() Method

Remove duplicate DOM nodes and sort them into document order.

uniqueSort() Method tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful