/ Published in: JavaScript
Array.prototype.forEach
You can't break forEach. So use "some" or "every".
Array.prototype.some
some is pretty much the same as forEach but it break when the callback returns true.
Array.prototype.every
every is almost identical to some except it's expecting false to break the loop.
You can't break forEach. So use "some" or "every".
Array.prototype.some
some is pretty much the same as forEach but it break when the callback returns true.
Array.prototype.every
every is almost identical to some except it's expecting false to break the loop.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
var ary = ["JavaScript", "Java", "CoffeeScript", "TypeScript"]; ary.some(function (value, index, _ary) { console.log(index + ": " + value); return value === "CoffeeScript"; }); // output: // 0: JavaScript // 1: Java // 2: CoffeeScript ary.every(function(value, index, _ary) { console.log(index + ": " + value); return value.indexOf("Script") > -1; }); // output: // 0: JavaScript // 1: Java