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.
Remember
$.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)
Pass an array-like list, a boolean callback, and optionally invert.
Start with an array (or array-like object with length).
Write a function that receives (element, index) and returns true or false.
Call $.grep(array, fn) to keep passing items.
Pass true as the third argument to keep failing items instead.
Piece
Required?
Role
array
Yes
Array-like object to search
function
Yes
Boolean test: (element, index) → Boolean
invert
No
If true, keep where callback is false
Return
—
New Array (source unchanged)
Pseudocode
Pseudocode
function grep(array, fn, invert):
result = []
for i from 0 to array.length - 1:
pass = fn(array[i], i) // must be boolean-ish
if invert:
keep = not pass
else:
keep = pass
if keep:
result.push(array[i])
return result // new array; original untouched
Reference
Cheat Sheet
Keep these facts handy while filtering with jQuery.
Input. Start from a list that may still contain 9s.
Test. Keep only values where a !== 9.
Reassign.arr = $.grep(...) replaces your variable with the filtered list.
Practice. Chain small filters instead of one complex callback when rules are independent.
Edge Cases & Pitfalls
Check these before filtering real data.
boolean
Return a boolean, not a value
Unlike $.map, returning a number or string does not transform the item — it is only tested for truthiness. Use true/false on purpose.
objects
Plain objects are not supported
$.grep needs array-like input. For object maps, use Object.values(obj) first, or iterate with $.each.
this
this is window
Inside the callback, this is the global object — not the current element. Use the first argument for the value.
native
Prefer Array.filter in modern JS
On true arrays, arr.filter(fn) is native and clear. Keep $.grep when you need invert or array-like input in a jQuery codebase.
Compatibility
Browser Support
jQuery.grep() is a data utility available since jQuery 1.0+ in all browsers that load jQuery. It does not depend on a special DOM API — only on jQuery being present.
✓ jQuery 1.0+ · All browsers with jQuery
jQuery.grep()
Works wherever jQuery runs in the browser. For modern pure-array code without jQuery, Array.prototype.filter is the native alternative.
100%Browser support
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
$.grep()Browsers
Bottom line: Use $.grep(arr, fn) in jQuery apps. Use arr.filter(fn) when you only need native array filtering.
Analysis
Performance Notes
Approach
Cost
Best for
$.grep(arr, fn)
O(n)
Filter once over n items
Chained greps
O(n + m + …)
Clear multi-step rules
arr.filter(fn)
O(n)
Native arrays without invert
One pass is enough for a single rule. Chain greps only when each step is easier to read — combine into one callback if the list is huge and you need every micro-optimization.
Remember
Key Takeaways
Role:$.grep(arr, fn) → new filtered array.
Callback: return true/false (not a new value).
Invert: third arg true keeps failed tests.
Safe: source array is not mutated.
One line: use $.grep to select items with a boolean test — flip with invert, and use $.map when you need to change values.
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.