DEV Community

Syed Faysel Ahammad Rajo
Syed Faysel Ahammad Rajo

Posted on

JavaScript Filter() method

JavaScript filer method creates a new array with all elements that pass the test or condition implemented by the provided function. Following example will make it more clear.

const nums = [1,2,3,4,5,6,7,8,9,10];
const even = nums.filter(n => n % 2 == 0);
//this will return an array with all even numbers
Enter fullscreen mode Exit fullscreen mode

Here in the filter method, we used an arrow function as arguments. What filter method does is, loop over the array or object and pass element through the provided function. If the condition is true then the output is added to the new array.

Top comments (0)