Short Circuit Array iterations with Array.some

ES5 has some nice features, but one thing that isn't immediately obvious is how to exit a loop early using one of the array iteration methods. Thankfully this is pretty simple using Array.prototype.some.

var stuff = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20];

var found = stuff.some(function findTen(number) {
  return number === 10;
});

console.log(found); // outputs true

In this example, as soon as the callback function findTen returns true, the loop stops.

Array.prototype.some returns true as soon as it's callback returns a truthy value, otherwise it returns false if the callback never returned a truthy value after iterating the array.