jQuery jQuery.inArray() Method

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Array search

What You’ll Learn

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

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

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.

📝 Syntax

General form of jQuery.inArray:

jQuery
jQuery.inArray( value, array [, fromIndex ] )
// or
$.inArray( value, array [, fromIndex ] )

Parameters

  • value — the item to search for. Compared with strict equality (===) against each element.
  • array — an array or array-like object to search through.
  • fromIndex (optional) — index at which to begin searching. Defaults to 0. A negative value counts from the end of the array.

Return value

  • The zero-based index of the first matching element.
  • -1 if value is not found.

Minimal workflow

jQuery
var names = ["Ann", "Bob", "Carl"];
var idx = $.inArray("Bob", names);

console.log(idx); // 1

⚡ Quick Reference

GoalCode
Find index$.inArray("John", arr)
Check membership$.inArray(val, arr) !== -1
Not found$.inArray("missing", arr) === -1
Search from index 2$.inArray("Pete", arr, 2)
Strict equality4 and "4" are different
Mutates original?No — read-only search

📋 $.inArray vs indexOf vs includes

Three ways to search — pick based on what you need back.

$.inArray
returns index

jQuery static; array-like input OK

indexOf
returns index

Native Array method only

includes
true / false

ES2016 membership check

$.grep
custom test

Filter by function, not exact value

Examples Gallery

Each example uses $.inArray(). Open DevTools or use the Try-it links to run them interactively.

📚 Getting Started

Find values in mixed arrays — the pattern from the official jQuery API docs.

Example 1 — Find Strings and Numbers in an Array

Search a mixed array for existing values and read back their indexes.

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

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

How It Works

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
Try It Yourself

How It Works

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
Try It Yourself

How It Works

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
Try It Yourself

How It Works

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.

jQuery
var allowedRoles = ["admin", "editor", "viewer"];
var userRole = "editor";

if ($.inArray(userRole, allowedRoles) !== -1) {
  console.log("Access granted for: " + userRole);
} else {
  console.log("Access denied");
}
// Access granted for: editor
Try It Yourself

How It Works

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.

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

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

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 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
jQuery.inArray() Universal

Bottom line: Safe in any project that already includes jQuery. On modern apps without jQuery, use Array.prototype.indexOf() or includes().

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.

💡 Best Practices

✅ Do

  • Compare explicitly: $.inArray(val, arr) !== -1
  • Use fromIndex to find later duplicates
  • Normalize types before searching mixed data
  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about jQuery.inArray()

Search arrays the jQuery way.

5
Core concepts
🚫 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.

Continue to jQuery.uniqueSort()

Remove duplicate DOM node references and sort elements 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.

6 people found this page helpful