The jQuery.inArray() utility searches an array for a value and returns its index — or -1 when the value is missing. This tutorial covers syntax, the optional fromIndex parameter, five worked examples, comparisons with indexOf() and includes(), and practical membership-check patterns.
01
Syntax
$.inArray(val, arr)
02
Index
0-based position
03
Not found
Returns -1
04
Strict
=== equality
05
fromIndex
Start later
06
Membership
!== -1
Fundamentals
Introduction
Searching a list for a specific value is one of the most common programming tasks. Is this ID in the allowed list? Which slot holds the user’s selection? jQuery provides jQuery.inArray() (also written $.inArray()) as a static utility that answers: where is this value?
Unlike jQuery.grep(), which filters with a custom function, inArray looks for one exact value using strict equality. The return value is a number — the index when found, or -1 when not found — matching the familiar behavior of Array.prototype.indexOf().
Concept
Understanding the jQuery.inArray() Method
jQuery.inArray(value, array [, fromIndex]) walks the array from left to right (starting at fromIndex when provided) and compares each element to value with ===. The first matching index is returned; if no match exists, the method returns -1.
The input collection can be a true array or any array-like object with length and indexed values — the same kinds of collections $.grep() and $.merge() accept.
💡
Beginner Tip
To check whether a value exists, write $.inArray(value, arr) !== -1. That reads as “the index is not negative one,” meaning a match was found.
jQuery scans from index 0 upward. "John" matches at index 3, the number 4 at index 0, and 8 at index 2. Only the first match is returned when duplicates exist.
📈 Practical Patterns
Handle missing values, type rules, partial searches, and real-world validation.
Example 2 — Value Not Found Returns -1
When the search value is absent, jQuery returns -1 — never throws an error.
jQuery
var arr = [4, "Pete", 8, "John"];
var idx = $.inArray("David", arr);
console.log(idx); // -1
console.log(idx === -1); // true — not in the array
Always compare against -1 explicitly. Because 0 is a valid index, patterns like if ($.inArray(val, arr)) fail when the match is at the first slot — index 0 is falsy in JavaScript.
Example 3 — Strict Equality: 4 vs "4"
inArray uses ===, so type matters — a common beginner gotcha.
jQuery
var arr = [4, "Pete", 8, "John"];
console.log($.inArray(4, arr)); // 0 — number 4 at index 0
console.log($.inArray("4", arr)); // -1 — string "4" is not strictly equal
Index 0 holds the number 4. The string "4" fails strict comparison, so the search continues and eventually returns -1. Coerce types first if your data mixes strings and numbers.
Example 4 — Start Searching with fromIndex
Pass a third argument to skip earlier indexes — from the official jQuery demo.
jQuery
var arr = [4, "Pete", 8, "John"];
console.log($.inArray("Pete", arr)); // 1 — found at index 1
console.log($.inArray("Pete", arr, 2)); // -1 — search starts at index 2, skips index 1
With fromIndex set to 2, jQuery never checks indexes 0 or 1. "Pete" lives at index 1, so the second call reports not found. Use this to find later duplicates or resume a search.
Example 5 — Validate Against an Allowed List
Check whether user input or a status code is permitted — a everyday UI pattern.
You often care about whether a value exists, not its index. The idiom !== -1 turns the numeric result into a membership test. For whitelists, tag pickers, and enum validation, this pattern is clear and fast.
Applications
🚀 Common Use Cases
Whitelist validation — confirm a role, status, or category string is allowed.
Tab or menu state — find which index matches the active item ID.
Duplicate detection — check whether a value already exists before pushing to an array.
Partial search — use fromIndex to locate a second occurrence.
Plugin configuration — test whether an option appears in a supported-options array.
Legacy jQuery codebases — consistent search before includes() was widely available.
🧠 How jQuery.inArray() Finds a Match
1
Set start index
Begin at fromIndex (default 0), resolving negative values from the end.
Start
2
Compare slots
Test array[i] === value at each index moving left to right.
Strict
3
First match wins
Return the index immediately when a strict match is found; stop scanning.
Return i
4
🔍
Or return -1
If every slot fails the test, return -1. The source array is unchanged.
Important
📝 Notes
Available since jQuery 1.2 — one of the core array utilities.
Uses strict equality (===) — no type coercion.
Never use the result as a bare boolean — index 0 is falsy.
Returns only the first match; duplicates after that are ignored unless you use fromIndex.
Works on array-like objects, not plain key/value objects.
For complex matching rules (partial strings, ranges), use $.grep() instead.
Compatibility
Browser Support
jQuery.inArray() has been part of jQuery since jQuery 1.2+. It works wherever jQuery runs — all modern browsers and legacy IE when paired with a supported jQuery build.
✓ jQuery 1.2+
jQuery jQuery.inArray()
Supported in jQuery 1.x, 2.x, and 3.x. Behavior mirrors Array.indexOf semantics with array-like input support.
100%With jQuery loaded
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
jQuery.inArray()Universal
Bottom line: Safe in any project that already includes jQuery. On modern apps without jQuery, use Array.prototype.indexOf() or includes().
Wrap Up
Conclusion
The jQuery.inArray() utility answers a simple question: where is this value in the array? It returns a zero-based index on success and -1 when the value is missing — the same contract as indexOf(), with jQuery’s array-like input support.
Remember strict equality, never treat the result as a bare boolean, and use !== -1 when you only need a yes/no membership check — then explore jQuery.uniqueSort() when merged DOM node lists need deduplication.
Prefer native includes() for simple checks in jQuery-free code
Combine with $.grep() when rules are more than exact match
❌ Don’t
Write if ($.inArray(val, arr)) — fails at index 0
Expect loose equality — "4" and 4 differ
Search plain objects — convert keys/values to an array first
Assume all duplicates are found — only the first match is returned
Use inArray when you need to filter by a custom condition
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about jQuery.inArray()
Search arrays the jQuery way.
5
Core concepts
🔍01
$.inArray
Returns index
API
🚫02
-1
Not found
Signal
📐03
===
Strict match
Compare
⏩04
fromIndex
Start later
Option
✅05
!== -1
Membership
Pattern
❓ Frequently Asked Questions
jQuery.inArray(value, array, fromIndex) searches an array or array-like object for value using strict equality (===). It returns the zero-based index where value is found, or -1 if value is not present.
jQuery follows the same convention as Array.prototype.indexOf: -1 means not found. Test membership with $.inArray(val, arr) !== -1, or use native arr.includes(val) in modern JavaScript.
Both use strict equality and return -1 when missing. indexOf is a native Array method. inArray is a jQuery static utility that also accepts array-like objects (with length and indexed values), which indexOf does not handle directly.
When provided, jQuery starts searching at that index instead of 0. Useful for finding later occurrences. Negative fromIndex values count backward from the end of the array, matching indexOf behavior in modern browsers.
Strict equality (===). The string "4" and the number 4 are different values — $.inArray("4", [4, "Pete"]) returns -1 because no slot strictly equals "4" at index 0 (which holds the number 4).
Use $.inArray() when you need the index position or when working in a jQuery codebase with array-like input. Use includes() for simple boolean membership checks on true arrays in modern code without jQuery.
Did you know?
The official jQuery demo for inArray uses the mixed array [4, "Pete", 8, "John"] and shows that $.inArray("Pete", arr, 2) returns -1 because the search starts at index 2 — after "Pete" at index 1. That one line teaches both return values and the fromIndex parameter.