On this page
  1. Array.filter
  2. Signature & Overview
  3. Notes & Pitfalls
  4. Example

Array.filter

Prev: map | Up: Array | Next: reduce
Rel: Array, Array & Typed Arrays Guide
Ref: MDN Web Docs — Array.filter
Tags: #public #web #javascript #builtins #array #tier/core


Signature & Overview

arr.filter(predicate(el, idx, arr))

Returns a new shallow-copied array containing only elements where predicate returns truthy.

Notes & Pitfalls

Example

const nums = [1, 2, 3, 4, 5, 6];
const evens = nums.filter((n) => n % 2 === 0);
console.assert(JSON.stringify(evens) === "[2,4,6]");

Linked from

Related notes