You could use the Array.prototype.some()
method :
The some() method tests whether some element in the array passes the
test implemented by the provided function.
It could be more efficient than forEach
method as it stops iterating (short circuit in a some way) as soon a element matches the condition.
For example to check that all elements are > 0
, use some()
with the reverse condition, that is : <=0
.
var isFailed = [0, 1, 2, 3, 4].some(x => x <= 0);
For example, here, as soon the first iteration, some()
exits and return false
.