numbers = [1,2,3,4,5,4,3,2,1];
var filterResult = numbers.filter(function(i){
return (i > 2);
});
I don't understand how this works. if I omit the i as a function argument it breaks the function but the i isn't tied to anything so why does it need to be there?
function(i) { return (i > 2); }and calls that function, passing the current value that is being processed as a parameter to that function. Since you named the first parameter that your function accepts "i", you're now able to work with the variable i inside that function. If it returns "true", the value will be in the new array that filter() returns, if it returns "false", it will be skipped. That way, you can easily "filter" an array.