Array.prototype.filter() — JS Technical Interview Questions & Answers

Builds a new array with elements that pass a test.

Array.prototype.filter()

  • Array.prototype.filter() is a JS topic. In plain words you use it for fail students only in a marks list. Don’t start with a slogan — start with that picture.
  • Smallest example: filter(m => m < 40). Type it, run it, and say what you see. If you can do that from memory, you know Array.prototype.filter().
  • From the example next to this theory: Builds a new array with elements that pass a test.
  • Trap: mutating while filtering. Fix that before you talk about advanced JS.
  • Viva: what is Array.prototype.filter()? Then show filter(m => m < 40). Then name the trap.

Technical Interview Tip: What is Array.prototype.filter()? Show this: filter(m => m < 40). Trap: mutating while filtering.

Code Examples for Array.prototype.filter()

Array.prototype.filter() — working example

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

Array.prototype.filter() — filter keeps elements where the callback returns truthy — returns a new array.

How to Explain Array.prototype.filter() to an Interviewer

What is Array.prototype.filter()?

Used for fail students only a marks list Not just the heading

Give one small example of Array.prototype.filter().

filter(m => m < 40) Builds a new array with elements that pass a test

What mistake do beginners make with Array.prototype.filter()?

mutating while filtering Show the fix

Where do you use Array.prototype.filter()?

a marks list fail students only