Need to check that every object in an array has a required property? Call every with a predicate: it returns true when all array items satisfy the condition, and false when at least one does not. The result is a single answer about the array, not a new array. section 10.1.4
Listing 10.7 Testing array item conditions with the every and some methods
const ninjas = [
{ name: "Yagyu", weapon: "shuriken" },
{ name: "Yoshi" },
{ name: "Kuma", weapon: "wakizashi" },
];
const allNinjasAreNamed = ninjas.every( #A
(ninja) => "name" in ninja, #A
); #A
const allNinjasAreArmed = ninjas.every( #A
(ninja) => "weapon" in ninja, #A
); #A
console.assert(allNinjasAreNamed, "Every ninja has a name");
console.assert(
!allNinjasAreArmed,
"But not every ninja is armed",
);
const someNinjasAreArmed = ninjas.some( #B
(ninja) => "weapon" in ninja, #B
); #B
console.assert(
someNinjasAreArmed,
"But some ninjas are armed",
);
The name check passes because every object has a name. The weapon check fails because one object lacks a weapon. The same listing contrasts every with some, which succeeds when at least one object is armed. section 10.1.4
Use every when the question is whether all items meet a criterion. Use some to ask whether any do; use filter when you need the matching items themselves. section 10.1.4 section 10.5




