Javascript | Sololearn: Learn to code for FREE!



How to use .filter() ?

7/3/2022 10:38:52 AM


3 Answers

New Answer


The filter() method creates a new array filled with elements that pass a test provided by a function. The filter() method does not execute the function for empty elements. The filter() method does not change the original array.


Filter is array method, and it will loop trought array and run some function per each item of array. This function you type yourself, in my code i used arrow function syntax (mostly this is used now, but you can use normal function also), this function mostly have if/else to check something (do filtering). Filter method at end return new filteted array, with items passed check. This is code to filter only even numbers from array, and I explained how it works step by step:


Just adding reference links cause you tagged both Javascript and CSS