Javascript | Sololearn: Learn to code for FREE!

+2

Javascript

How to use .filter() ?

7/3/2022 10:38:52 AM

Sachin

3 Answers

New Answer

+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.

+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

+2

Just adding reference links cause you tagged both Javascript and CSS https://developer.mozilla.org/en-US/docs/Web/CSS/filter https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter