Array.prototype.every looks to be different than what you want:
The every method executes the provided callback function once for each element present in the array until it finds one where callback returns a falsy value. If such an element is found, the every method immediately returns false. Otherwise, if callback returns a truthy value for all elements, every returns true. callback is invoked only for indexes of the array which have assigned values; it is not invoked for indexes which have been deleted or which have never been assigned values.
var tableRows = [1, 2, 3, 4, 5, "word"];
var tableRows2 = [1, 2, 3, 4, 5];
var everyTest = tableRows.every(function(currentValue, index, array) {
return (typeof currentValue == "number")
});
var everyTest2 = tableRows2.every(function(currentValue, index, array) {
return (typeof currentValue == "number")
});
console.log(everyTest);
console.log(everyTest2);
You probably wanted to use an Array.prototype.forEach. Sadly there is no way to break from a forEach short of throwing an exception, which sounds like your dilemma.
var tableRows = [1, 2, 3, 4, 5, "words"];
var forEachTest = tableRows.forEach(function(currentValue, index, array) {
console.log(currentValue);
});
Although forEach
doesn't have a break
, the traditional for
loop does!
var tableRows = [1, 2, 3, 4, 5, "words", 6, 7, 8];
for (var i = 0; i < tableRows.length; i++) {
var current = tableRows[i];
if(typeof current != 'number')
break;
console.log(current);
}