JavaScript Filter

filter() runs a test on every element and returns a new array containing only the elements for which the callback returned a truthy value. The original array is left untouched.

Syntax

arr.filter((x) => x > 2)

Examples

Input Arguments Output
[1, 2, 3, 4] x => x % 2 === 0 [2, 4]
["a", "bb", "ccc"] x => x.length > 1 ["bb", "ccc"]

When to use it

  • Keep only the entries matching a search or a condition.
  • Remove empty or invalid values from a list.

Gotchas

  • An empty result is [] (an empty array), never null.
  • filter() answers "which ones?". Use find() when you only need the first match.

Try it live

Type your input and see Filter transform it instantly.

Filter()

Type above to see the result…

Want to go further? Chain Filter with other functions, pipe one output into the next and watch your data transform in the visual Playground.

Related functions

© 2026 Heifara Buval