jQuery grep() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Utilities

Definition

jQuery.grep(array, function[, invert]) (alias $.grep) keeps items that pass a boolean test (since jQuery 1.0). Optional invert flips the test — keep where the callback returns false.

Syntax

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

jQuery
$.grep(array, function(element, index) {
  return true;   // keep this element
})

$.grep([0, 1, 2], function (n) { return n > 0; })
// → [1, 2]

$.grep([0, 1, 2], function (n) { return n > 0; }, true)
// → [0]   (invert)
CallWhat it does
arrayArray-like object to search
functionBoolean test: (element, index) → Boolean
invertIf true, keep where callback is false
ReturnNew Array (source unchanged)

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

Parameters

ParameterDescription
Step 1Start with an array (or array-like object with length).
Step 2Write a function that receives (element, index) and returns true or false.
Step 3Call $.grep(array, fn) to keep passing items.

Return Value

Return ValueDescription
BooleanOften true or false depending on the match or state.

jQuery Examples

Example 1 — Keep Numbers Greater Than Zero

Official simple filter: drop zeros and negatives.

jQuery
var result = $.grep([0, 1, 2], function (n) {
  return n > 0;
});

console.log(result);  // [1, 2]
Try It Yourself

How It Works

  1. Source. Start with [0, 1, 2].
  2. Test. Callback returns true only when n > 0.
  3. Keep. 1 and 2 pass; 0 fails.

Example 2 — Invert the Test

Same callback, opposite keep rule with invert: true.

jQuery
var result = $.grep([0, 1, 2], function (n) {
  return n > 0;
}, true);

console.log(result);  // [0]
Try It Yourself

How It Works

  1. Same test. Callback still asks n > 0.
  2. Third argument. true means keep where the callback is false.
  3. Keep failures. Only 0 fails n > 0.

Example 3 — Filter with Value and Index

Use both callback arguments, like the official demo.

jQuery
var arr = [1, 9, 3, 8, 6, 1, 5, 9, 4, 7];

var result = $.grep(arr, function (n, i) {
  return n !== 5 && i > 4;
});

console.log(result);  // [1, 9, 4, 7]
Try It Yourself

How It Works

  1. Two args. n is the value; i is its index.
  2. Index rule. i > 4 drops the first five slots.
  3. Value rule. n !== 5 drops the 5 at index 6.

Frequently Asked Questions

jQuery.grep(array, callback) returns a new array of elements for which your callback returns true. The original array is never modified. It is a static filter utility for array-like collections.
A boolean. Return true to keep the element (when invert is false or omitted). Return false to drop it. Unlike $.map(), you do not return transformed values — only a pass/fail decision.
When invert is true, $.grep() keeps elements where the callback returns false instead of true. Use it when the rejection rule is simpler to write than the keep rule.
With invert false (the default), $.grep() behaves like Array.filter() on arrays. $.grep() also accepts array-like objects with a length property and adds the optional invert flag. Array.filter() is native and has no invert option.
No. Unlike $.each() or $.map(), $.grep() only searches array-like objects — arrays and objects with numeric indexes and a length. Filter Object.values(obj) if you need object data.
$.grep() selects existing elements with a boolean test and returns a subset of the same values. $.map() transforms every item and can change values, skip with null, or flatten nested arrays.
Yes, if jQuery is loaded in that environment. $.grep() is a pure data utility — it does not need a DOM. Many Node setups still prefer native Array.filter() instead.

Next: jQuery.extend() Method

Merge objects and defaults with jQuery’s extend utility.

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