CodingNeed.

4 · Build useful programs · 9 MIN

Select matching items

Filtering keeps items that meet a rule.

A predicate is a function returning true or false. filter calls it for each item and creates a new array containing the matches. The original array stays unchanged. This scans n items in O(n) time and stores up to n matches.

A sieve keeps only the items that fit a rule.

Read the example

const scores = [30, 60, 90];
const passed = scores.filter(n => n >= 60);
console.log(JSON.stringify(passed));
Check the expected output
[60,90]

Your challenge

Return a new array containing only the even numbers from input, preserving order.

Common trap

Use === for comparison. = is assignment.

Next lesson: Describe a real thing →

Essential cookies keep your account signed in. Optional analytics is not configured on this site. Your choice does not affect access to lessons.

Read the Privacy Policy. You can change this choice in the footer.