🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEjavascript

javascript Documentation

LOADING ENGINE...

Array.filter()

AI & DATA SCIENCE // filter

filter() creates a new array with all elements that pass a test (return true from the callback).

Syntax

const evens = [1,2,3,4,5].filter(n => n % 2 === 0);
// [2, 4]

Deep Dive Course

**filter()** returns a new array containing only elements for which the callback returns a truthy value. It never mutates the original. Use it to select subsets of data. Combine with **map()** for transform-then-select pipelines.

1Understanding Array.filter()

filter() returns a new array containing only elements for which the callback returns a truthy value. It never mutates the original. Use it to select subsets of data. Combine with map() for transform-then-select pipelines.

💡

Boolean can be used as the filter callback to remove all falsy values: arr.filter(Boolean) removes null, undefined, 0, '', false, NaN.

editor.html
const people = [
  { name: 'Alice', age: 30, active: true  },
  { name: 'Bob',   age: 17, active: true  },
  { name: 'Carol', age: 25, active: false },
];

const activeAdults = people
  .filter(p => p.active && p.age >= 18)
  .map(p => p.name);
console.log(activeAdults); // ['Alice']
localhost:3000

2Practical Example

Here is a real-world application of Array.filter() showing how it is used in production JavaScript code.

editor.html
// Remove falsy values
const mixed = [1, null, 2, undefined, 3, '', 4, false, 5];
const clean = mixed.filter(Boolean);
console.log(clean); // [1, 2, 3, 4, 5]
localhost:3000

3Best Practices

Follow these guidelines when working with Array.filter():

1. Use filter(Boolean) to remove falsy values

2. Chain filter().map() for select-then-transform

3. Use find() if you only need the first matching element

⚠️

Tip: Boolean can be used as the filter callback to remove all falsy values: arr.filter(Boolean) removes null, undefined, 0, '', false, NaN.

editor.html
const people = [
  { name: 'Alice', age: 30, active: true  },
  { name: 'Bob',   age: 17, active: true  },
  { name: 'Carol', age: 25, active: false },
];

const activeAdults = people
  .filter(p => p.active && p.age >= 18)
  .map(p => p.name);
console.log(activeAdults); // ['Alice']
localhost:3000

Examples

Example 01Basic Usage
const people = [
  { name: 'Alice', age: 30, active: true  },
  { name: 'Bob',   age: 17, active: true  },
  { name: 'Carol', age: 25, active: false },
];

const activeAdults = people
  .filter(p => p.active && p.age >= 18)
  .map(p => p.name);
console.log(activeAdults); // ['Alice']
Example 02Advanced Example
// Remove falsy values
const mixed = [1, null, 2, undefined, 3, '', 4, false, 5];
const clean = mixed.filter(Boolean);
console.log(clean); // [1, 2, 3, 4, 5]

Best Practices

  • Use filter(Boolean) to remove falsy values
  • Chain filter().map() for select-then-transform
  • Use find() if you only need the first matching element

Interview Question

What is the return value of filter() when nothing matches?

Hint: An empty result is still an array.

filter() always returns an array. If no elements match, it returns an empty array []. It never returns null or undefined. This makes filter() safe to chain without null checks.

Exercises

MediumPractice using Array.filter() in a real scenario.
View Solution
const people = [
  { name: 'Alice', age: 30, active: true  },
  { name: 'Bob',   age: 17, active: true  },
  { name: 'Carol', age: 25, active: false },
];

const activeAdults = people
  .filter(p => p.active && p.age >= 18)
  .map(p => p.name);
console.log(activeAdults); // ['Alice']

Frequently Asked Questions

What is the return value of filter() when nothing matches?

filter() always returns an array. If no elements match, it returns an empty array []. It never returns null or undefined. This makes filter() safe to chain without null checks.

Related Functions

mapreducefindforEach