Arrays

Filtering Arrays in JavaScript: The Essential Guide

The filter() Method

filter() creates a new array containing only the elements that pass a given test (callback function returning true).

let numbers = [1, 2, 3, 4, 5, 6];
let evens = numbers.filter(num => num % 2 === 0);
console.log(evens); // [2, 4, 6]

Filtering Objects in an Array

let users = [
    { name: "Alice", age: 25 },
    { name: "Bob", age: 17 }
];
let adults = users.filter(user => user.age >= 18);
console.log(adults); // [{ name: "Alice", age: 25 }]

filter() does not modify the original array — it always returns a brand-new array.

Ready to master real-world JavaScript development?

Learn JavaScript hands-on with mentor-led, live sessions.

Explore Course