Filtering Arrays with filter
The filter method is a powerful tool in JavaScript for creating a new array that includes only those elements from an existing array that satisfy a specific condition. When you use filter, you provide a callback function that tests each element. If the callback returns true for an element, that element is included in the new array; if it returns false, the element is excluded. This makes filter especially useful for extracting subsets of data, such as finding all users over a certain age, selecting completed tasks, or narrowing down items based on search criteria.
12345const numbers = [1, 2, 3, 4, 5, 6]; const evenNumbers = numbers.filter(function(num) { return num % 2 === 0; }); console.log(evenNumbers); // Output: [2, 4, 6]
One important aspect of filter is that it does not modify the original array. Instead, it returns a new array containing only the elements that passed the test. This concept is called immutability. By leaving the original array unchanged, filter helps prevent unexpected side effects in your code, making it easier to reason about data flow and transformations.
Note
You can also chain
filterwith other array methods, such asmaporsort, to perform more advanced transformations in a single, readable line of code. This makes it easy to build complex data processing pipelines while keeping your code clean and expressive.
1. Which of the following will be the output of the code below?
2. Complete the callback function to filter an array of numbers, returning only those greater than 10.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 8.33
Filtering Arrays with filter
Swipe to show menu
The filter method is a powerful tool in JavaScript for creating a new array that includes only those elements from an existing array that satisfy a specific condition. When you use filter, you provide a callback function that tests each element. If the callback returns true for an element, that element is included in the new array; if it returns false, the element is excluded. This makes filter especially useful for extracting subsets of data, such as finding all users over a certain age, selecting completed tasks, or narrowing down items based on search criteria.
12345const numbers = [1, 2, 3, 4, 5, 6]; const evenNumbers = numbers.filter(function(num) { return num % 2 === 0; }); console.log(evenNumbers); // Output: [2, 4, 6]
One important aspect of filter is that it does not modify the original array. Instead, it returns a new array containing only the elements that passed the test. This concept is called immutability. By leaving the original array unchanged, filter helps prevent unexpected side effects in your code, making it easier to reason about data flow and transformations.
Note
You can also chain
filterwith other array methods, such asmaporsort, to perform more advanced transformations in a single, readable line of code. This makes it easy to build complex data processing pipelines while keeping your code clean and expressive.
1. Which of the following will be the output of the code below?
2. Complete the callback function to filter an array of numbers, returning only those greater than 10.
Thanks for your feedback!