Difference between every and filter in javascript?
The functions do completely different things.
Array.prototype.filter
will create an array of all the elements matching your condition in the callback
function isBigEnough(element) {
return element >= 10;
}
var filtered = [12, 5, 8, 130, 44].filter(isBigEnough);
// filtered is [12, 130, 44]
Array.prototype.every
will return true if every element in the array matches your condition in the callback
function isBigEnough(element, index, array) {
return (element >= 10);
}
var passed = [12, 5, 8, 130, 44].every(isBigEnough);
// passed is false
passed = [12, 54, 18, 130, 44].every(isBigEnough);
// passed is true
Array.prototype.filter
returns an array of the matched data from the given array.
Array.prototype.every
will always return boolean value according to the given conditions.