How does the filter function work in js? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 13

How does the filter function work in js?

var c = newArray(1,2,3,4); var b = c.filter(function(a){return(a%2)}); alert(b[0]+b[1]) //the answer is 4

18th May 2019, 8:38 AM
Tahir Usman
Tahir Usman - avatar
4 Answers
+ 10
ThanksTheWhiteCat 😍
18th May 2019, 8:49 AM
Tahir Usman
Tahir Usman - avatar
+ 5
Filter method creates new array with elements that pass certain condition. In this case filter the even elements of the array. So b => [1, 3] and b[0]+b[1] is 4. You can find more info here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter. Hope it helps you.
18th May 2019, 8:44 AM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 1
Tahir Usman , you are welcome 😉
18th May 2019, 8:50 AM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar