← All tutorials

GoCareerGo Tutorials

JavaScript Tutorial

Variables to closures, promises, the DOM and OOP — a complete JS reference.

Array.prototype.filter()

Creates a new array containing only the elements that pass a test function.

const nums = [1, 2, 3, 4, 5];
const evens = nums.filter(n => n % 2 === 0);
// evens: [2, 4]
  • The callback should return true (keep) or false (discard)
  • Returns a new array — the length can be shorter than the original
  • Commonly chained with .map() to transform and narrow data in one pipeline