A callback function is a function passed to another function so that function can call it during its work. JavaScript’s array method filter() uses a callback as a test: it keeps each array element for which the callback returns a truthy value and returns those elements in a new array.
What is a callback function?
A callback is a function supplied to another function for the receiving function to invoke. The name describes how the function is used: it is passed in, then called by the function that receives it.
As an Amazon Associate I earn from qualifying purchases.
With filter(), the callback decides whether each array element belongs in the result.
Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchWindows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallHow does Array.filter() work?
filter() calls its callback once for each present element in the array. If the callback’s result is truthy, that element is included; if the result is falsy, it is left out. The method returns a shallow copy containing the selected values. It does not return the callback or an array of true and false results.
#1 Best Overall
const numbers = [12, 5, 8, 130, 44];
const largeNumbers = numbers.filter((value) => value >= 10);
// largeNumbers is [12, 130, 44]
In this example, (value) => value >= 10 is the callback. For each value, the comparison produces a Boolean result, and filter() retains the values that pass it. If no elements pass, the result is an empty array.
How do you pass a named callback?
You can define the test as a named function and pass its name to filter():
Rank #2
function isBigEnough(value) {
return value >= 10;
}
const largeNumbers = numbers.filter(isBigEnough);
Use numbers.filter(isBigEnough), not numbers.filter(isBigEnough()). The first passes the function so filter() can call it for each element. The second calls the function immediately and passes its return value instead.
What arguments can the callback receive?
The method signature is filter(callbackFn) or filter(callbackFn, thisArg). The callback can receive the current element, its index, and the original array, in that order:
array.filter((element, index, array) => {
// return a truthy value to keep element
});
elementis the current value being tested.indexis that value’s position.arrayis the original array passed tofilter(), not the result array being built.
Use only the parameters you need. The optional thisArg supplies the value used as this inside a non-arrow callback; arrow functions use lexical this instead.
Does filter() change the original array?
No. filter() creates a new array and does not change the original array’s membership or order. The copy is shallow: if the selected elements are objects, the result contains references to those same objects rather than deep copies.
Rank #4
How does filter() handle empty slots?
For a sparse array, filter() skips empty slots rather than calling the callback for them. An explicit undefined value is an element, so the callback is called for it. This distinction matters when the callback checks for undefined values or counts visits.
Free tools Windows power users keep installed
One-click scans. No signup required.
When should you use filter() instead of finding one item?
Use filter() when you want all elements that pass a test; it checks the present elements and builds an array of all matches. If you need one matching item instead of a selected array, choose an operation designed to return a single match. filter() should not be treated as stopping at the first match.
Best Value
Can filter() work on array-like objects?
Yes. filter() is generic: it can operate on an object with a length property and integer-keyed properties. Its behavior and callback rules are documented in MDN’s Array.prototype.filter() reference.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




