iteration

Array.prototype.every

Returns true only if the callback returns truthy for every element, short-circuiting on the first falsy result. Reach for it to validate that all items meet a condition.

arr.every(callbackFn[, thisArg])

Parameters

Parameter Purpose
callbackFn(element, index, array) Predicate tested against each element
thisArg Value used as `this` inside callbackFn

Examples

console.log([2,4,6].every(n => n % 2 === 0));

Logs: true

console.log([2,4,7].every(n => n % 2 === 0));

Logs: false

console.log([].every(() => false));

Logs: true (empty arrays are vacuously true)

console.log([{ok:true},{ok:true}].every(o => o.ok));

Logs: true

Gotcha

every on an empty array is TRUE (vacuous truth). Combine with `.length > 0` if that semantic is wrong for you.

Related methods

← All JS array methods · TypeScript vs JavaScript