Interview Question

What is filter()?

filter selects matching elements into a new array.

💡 Concept ✅ Quick Revision ⚡ JavaScript

Answer

filter creates a new array containing elements whose callback result is truthy. • The result may be shorter than the source array. • It does not mutate the source array by itself. • The callback receives value, index, and the source array.

Example

Code
console.log([1, 2, 3, 4].filter(value => value % 2 === 0));
Output
[2, 4]

Quick Revision

filter selects matching elements into a new array.