Difference between JavaScript Array every and some

The documentation answers your question...

The some() method tests whether some element in the array passes the test implemented by the provided function.

The every() method tests whether all elements in the array pass the test implemented by the provided function.

So you will use them, according if you want to test some elements or every elements.

If every() returns true then some() returns true.

but

If some() returns true then we cannot conclude anything about the result of every().


some is analogue to logical or
every is analogue to logical and

logically every implies some, but not in reverse

try this:

var identity = function(x){return x}
console.log([true, true].some(identity))//true
console.log([true, true].every(identity))//true
console.log([true, false].some(identity))//true
console.log([true, false].every(identity))//false
console.log([false, false].some(identity))//false
console.log([false, false].every(identity))//false
console.log([undefined, true].some(identity))//true
console.log([undefined, true].every(identity))//false
console.log([undefined, false].some(identity))//false
console.log([undefined, false].every(identity))//false
console.log([undefined, undefined].some(identity))//false
console.log([undefined, undefined].every(identity))//false

Array.prototype.some is good if you are looking for an intruder or a relic, meaning you only need to know if there is one at least that satisfies your constraints.

Array.prototy.every on the other hand is useful to check the integrity of an array, for example, "Are all items of my array an instance of Car?".


(If you know C# LINQ , it's like Any vs All)

  • some will return true if any predicate is true

  • every will return true if all predicate is true

Where predicate means function that returns bool ( true/false) for each element

every returns on first false.
some returns on first true