jQuery inArray() Method

Beginner
6 min read
Updated: Oct 2026
4 examples
Live preview

What Is inArray()?

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.

Remember
$.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
NeedUse
Find index$.inArray(val, arr)
Is value present?$.inArray(val, arr) !== -1
Native indexarr.indexOf(val)
Native booleanarr.includes(val)

How to Use inArray()

Pass the value, the array, and optionally where to start.

  1. Have an array (or array-like list) to search.
  2. Call $.inArray(value, array).
  3. If the result is -1, the value was not found.
  4. To start later in the list, pass fromIndex as the third argument.
PieceRequired?Role
valueYesValue to search for (strict ===)
arrayYesArray (or array-like) to search
fromIndexNoStart index (default 0)
Return—Index number, or -1

Pseudocode

Pseudocode
function inArray(value, array, fromIndex):
    start = fromIndex or 0
    for i from start to array.length - 1:
        if array[i] === value:   // strict equality
            return i
    return -1

// Membership check (safe):
found = inArray(value, array) !== -1

Cheat Sheet

Keep these facts handy while searching arrays with jQuery.

TopicDetail
SyntaxjQuery.inArray(value, array[, fromIndex])
SincejQuery 1.2
FoundZero-based index (0 is valid)
Missing-1
EqualityStrict (===)
Membership$.inArray(v, a) !== -1
Native twinsindexOf / includes

Live Preview

Search [4, "Pete", 8, "John"] with the official demo cases.

Browser demo of $.inArray() — same idea as the examples below.

Result
arr = [4, "Pete", 8, "John"]
$.inArray("John", arr)
→ 3

jQuery Examples

Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.

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.
  4. Remember. 0 means found at the start — not “missing”.

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.
  4. Avoid. Never rely on truthiness of the index alone.

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.
  4. Result. Returns -1 even though the value exists earlier.

Example 4 — Strict Equality Trap

Numbers and numeric strings are not the same.

jQuery
// Official-style strict check
console.log($.inArray(5 + 5, ["8", "9", "10", 10 + ""]));
// → -1  (looking for number 10 in an array of strings)

console.log($.inArray("10", ["8", "9", "10"]));  // 2
Try It Yourself

How It Works

  1. Left side. 5 + 5 is the number 10.
  2. Right side. 10 + "" becomes the string "10".
  3. Strict. 10 === "10" is false, so the first search fails.
  4. Match types. Searching for "10" finds index 2.

Edge Cases & Pitfalls

Check these before using indexes in real code.

falsy

Never treat the index as a boolean

0 == false in loose checks. Always write !== -1 (or > -1) for membership.

strict

Types must match

4 and "4" are different. Normalize types before you search if your data is mixed.

fromIndex

Earlier matches are skipped

A value can exist in the array and still return -1 if it only appears before fromIndex.

native

Prefer native helpers in modern JS

Use indexOf for indexes and includes for boolean checks on true arrays. Keep $.inArray in jQuery-heavy code.

Browser Support

jQuery.inArray() is a data utility available since jQuery 1.2+ in all browsers that load jQuery. It does not need a special DOM API — only jQuery.

✓ jQuery 1.2+ · All browsers with jQuery

jQuery.inArray()

Works wherever jQuery runs in the browser. For modern pure-array code, Array.prototype.indexOf and includes are the native alternatives.

100% Browser support
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
$.inArray() Browsers

Bottom line: Use $.inArray(val, arr) in jQuery apps. Use arr.indexOf(val) or arr.includes(val) for native arrays.

Performance Notes

ApproachCostBest for
$.inArray(val, arr)O(n)Small/medium lists
arr.indexOf(val)O(n)Native arrays
Set.has(val)O(1) avgMany membership checks

Linear scan is fine for typical UI arrays. If you check membership thousands of times on the same list, build a Set once instead of repeating inArray.

Key Takeaways

  • Role: $.inArray(val, arr) → index or -1.
  • Membership: always compare with !== -1.
  • Strict: types must match (===).
  • fromIndex: search can skip earlier matches.

One line: use $.inArray for a strict index lookup — treat -1 as missing, and never treat 0 as false.

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

Remove duplicate DOM nodes and sort them into document order.

uniqueSort tutorial →

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