Checking if every array element matches a condition
JavaScriptAll snippets in this category →
Posted on To check if every element matches a condition, arrays in JavaScript have the appropriately named Array.prototype.every()
.
const isEverybodyOver18 = ages => ages.every(age => age > 18)
isEverybodyOver18([27, 35, 52, 91]) // ⇒ true
isEverybodyOver18([16, 23, 42, 19]) // ⇒ false
const isEverybodyOver18 = ages => ages.every(age => age > 18)
isEverybodyOver18([27, 35, 52, 91]) // ⇒ true
isEverybodyOver18([16, 23, 42, 19]) // ⇒ false
const isEverybodyOver18 = ages => ages.every(age => age > 18)
isEverybodyOver18([27, 35, 52, 91]) // ⇒ true
isEverybodyOver18([16, 23, 42, 19]) // ⇒ false
const isEverybodyOver18 = ages => ages.every(age => age > 18)
isEverybodyOver18([27, 35, 52, 91]) // ⇒ true
isEverybodyOver18([16, 23, 42, 19]) // ⇒ false