Difference between .some() and .filter()?

Hi all, when going through the Iterator Documentation exercise, I’m still not fully understanding when you would use .some() versus .filter() since they seem to perform the same result. I read the MDN docs, but wondering if anyone has a different example they can point to, or provide a more practical explanation?
Thank you!

The two methods aren’t interchangeable and don’t give the same result. The .some method returns a boolean (true/false), while the .filter returns an array of elements which meet some condition. The .some method answers the question “Is there ANY element which meets the condition?”, whereas the .filter method answers the question “Which elements meet the condition?”

The .some method tests each element of the array against the condition. As soon as some element meets the condition, the .some method returns TRUE without checking the remaining elements. It doesn’t tell us how many elements meet the condition (one? some? many? all?) and doesn’t tell us which element(s) satisfy our condition, so .some can’t be used as a substitute for .filter, If no elements meet the condition, .some returns FALSE.

The .filter method will test every single element against the condition. A new array consisting of those elements which passed the test is returned. If no element passes the test, then an empty array is returned.

.some can’t be used as a substitute for .filter as explained above. .filter can be used to deliver the same functionality as .some, but at the cost of extra lines of code and inefficiency. After .filter returns the result array, we would have to check whether the array is empty or not (will need extra lines of code) to get a true/false answer. Also, .filter will check every single element of the array before returning the result array. This is inefficient if our goal is only to check whether one/some element in the whole array meets our condition. For that goal, it would be faster if we got an answer at the very first element that passes the test. Only in the worst-case scenario of no element passing the test would .some have traversed the whole array, whereas .filter would traverse the whole array in all scenarios.

For example, if we had an array [1, 5, 10, 12, 67, 5, 9, 20] and we wanted to test for even numbers, then as soon as .some reaches 10, it will stop and return TRUE (yes there is at least one even number in the array).
On the other hand, .filter will go through the whole array testing each element and then give the result [10, 12, 20]

Perhaps you are confused that in the exercise you mentioned, true and true are being logged to the console. That is not because .some and .filter are giving the same result. The first true is the answer from .some that yes, there is at least one element which has less than 6 characters. In the exercise, we then use .filter to select elements which have more than 5 characters and save the resulting array in interestingWords. In the exercise, we don’t log this array to the console. Instead the .every method is used to confirm/demonstrate that yes all elements of interestingWords meet our condition. That is where the second true in the console comes from.

2 Likes

Thank you for the thorough explanation! I think it was this particular exercise that was making me confused. Appreciate the additional examples!

1 Like

Thanks I didn’t know that