October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

JavaScript filter(): Choose Callback Tests to Keep Array Items

A callback is a function passed for another function to call. See how JavaScript’s Array.filter() uses callbacks to build an array of matching values.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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

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

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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
});
  • element is the current value being tested.
  • index is that value’s position.
  • array is the original array passed to filter(), 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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

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.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.