David Shin
2 min readNov 16, 2020

--

Array method array.every() VS array.some() method

Let’s talk about the array of methods that I learned from a recent study. It’s called every() and some() method. It does work as its name labeled, and pretty easy type of an array method to understand. So let’s show you some examples of how it works.

Array.porototype.every()

The way every() method returns either true/false if all the elements in the array contain truth/false values.

array.every(CB(currentValue, index, array), thisArg);
//CB => call back function

Unlikely some() method check is any items from the array or at least one item contains either true/false then from the result of the call back function the method return with truth/false value.

array.some(CB(currentValue, index, array), thiArg);

Both array actually works like array.filter() and array.map() method, because the methods have a call back function, and thisArg as a parameter, and inside the callback function it takes an argument as currentValue, index(optional), array(optional).

Ok, let’s implement this with an example.

const number = [1,2,3,4,5]function lessThanTwo(nums) {
return nums < 2 ? true : false
}
function lessThanSix(nums){
return nums < 6 ? true: false
}
console.log(number.every(lessThanTwo)); //false
console.log(number.every(lessThanSix)); //true
console.log(number.some(lessThanTwo)); //true;
console.log(number.some(lessThanSix)); //true;

The result showed than false, true, true, and then true. First, every() return false because, in order that passes true value, all the numbers in the array have to be less than two but because 2,3,4,5 is not less than two it returns false. Compare to the second call with the first one, we can learn that because the elements in the array are less than six and it meets the true statement for the criteria the call back function returns a true value.

What about some method() even though all the numbers in the array is not less than two we can still see that function is still passing result with true value and obviously the fourth call back function which is less than six too.

With these two array methods, we learned how it works differently and how can it be beneficial when using it for searching whether the elements in the array have the value that we are searching for or not.

--

--