Javascript | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Javascript

How to use .filter() ?

3rd Jul 2022, 10:38 AM
Sachin
Sachin - avatar
3 Answers
+ 6
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.
3rd Jul 2022, 10:57 AM
Mihir Lalwani
Mihir Lalwani - avatar
+ 2
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: https://code.sololearn.com/WFA08TpNzURi/?ref=app
3rd Jul 2022, 10:59 AM
PanicS
PanicS - avatar
3rd Jul 2022, 11:11 AM
Ipang