PHP array_filter() Equivalent in JavaScript

JavaScript's Array.prototype.filter() is a direct, eager equivalent of PHP's array_filter() when you always pass a callback — PHP's no-callback shorthand has no built-in one-liner in JS.

Close equivalent Covers the common case, but has documented behavioral differences.

PHP source
array_filter()
Closest JavaScript API
Array.prototype.filter()

Main limitation: PHP's `array_filter($arr)` with no callback strips falsy values automatically; JavaScript has no built-in one-argument equivalent — you must pass an explicit predicate like `.filter(Boolean)`, which uses different (but similar) truthiness rules.

How to use each function

Before comparing the two, here's how array_filter() and Array.prototype.filter() work on their own.

PHP: array_filter()

array_filter(array $array, ?callable $callback = null, int $mode = 0): array

Iterates over an array and keeps only the elements for which the callback returns a truthy value. With no callback, it removes all PHP-'falsy' elements (false, 0, '', null, []). The optional $mode flag lets the callback receive the key instead of, or in addition to, the value.

Returns: A new array containing only the elements that passed the test, preserving original keys.

PHP
$scores = [0, 85, false, 92, null, 78];
$valid = array_filter($scores);
// [1 => 85, 3 => 92, 5 => 78]

JavaScript: Array.prototype.filter()

arr.filter(callbackFn(element, index, array), thisArg)

Returns a new array containing only the elements for which callbackFn returns a truthy value, evaluated eagerly. There's no callback-less shorthand like PHP's array_filter($arr) — use .filter(Boolean) to drop falsy values — and the result is always reindexed from 0.

Returns: A new array, reindexed from 0.

JavaScript
[0, 85, false, 92, null, 78].filter(Boolean);
// [85, 92, 78]

Quick mapping

PHP array_filter()
Closest JavaScript API Array.prototype.filter()

How it differs

  • PHP's array_filter($arr) with no callback strips falsy values automatically; JavaScript has no built-in one-argument equivalent — you must pass an explicit predicate like .filter(Boolean), which uses different (but similar) truthiness rules.
  • PHP preserves original array keys after filtering; JavaScript's .filter() always returns a new array reindexed from 0, discarding the original indices entirely, matching Python's behavior more than PHP's.
  • "0" (the string zero) is falsy in PHP but truthy in JavaScript — a values array containing "0" will filter differently between array_filter($arr) and .filter(Boolean).

Examples

Keeping only even numbers

PHP
$evens = array_filter([1, 2, 3, 4], fn($n) => $n % 2 === 0);
// [1 => 2, 3 => 4]
JavaScript
const evens = [1, 2, 3, 4].filter((n) => n % 2 === 0);
// [2, 4]
Both are eager; JavaScript reindexes the result array from 0 while PHP keeps the original keys.

Removing falsy values

PHP
$clean = array_filter([0, 1, false, 2, "", 3]);
// [1 => 1, 3 => 2, 5 => 3]
JavaScript
const clean = [0, 1, false, 2, "", 3].filter(Boolean);
// [1, 2, 3]
`.filter(Boolean)` is the closest JavaScript equivalent to PHP's no-callback array_filter(); both drop 0, false, and empty string here, but always double-check `"0"` and NaN cases against your data.

Edge cases

Edge case

JavaScript's .filter(Boolean) removes 0, "", null, undefined, NaN, and false — but keeps "0" and [], both of which PHP's no-callback array_filter() would also keep ([] is falsy in PHP but empty arrays as *elements* being filtered are compared as values, so verify against your specific data).

References

  • PHP Manual — array_filter — checked 2026-07-29
  • MDN Web Docs — Array.prototype.filter() — checked 2026-07-29

Last verified 2026-07-29.