liveBook · Manning

All

As explained in Secrets of the JavaScript Ninja, Third Edition

A JavaScript method that returns true if every array element satisfies a predicate.

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

How Secrets of the JavaScript Ninja, Third Edition covers it

How deep it goes
Demonstrates predicate callbacks and true-or-false results with object properties, without exploring implementation internals.
Examples
Checks whether every object has a name or a weapon, alongside a some check for weapons.
What it assumes
Assumes familiarity with arrays, callbacks, and object-property checks rather than teaching those constructs from zero.
What you can do after
Apply it via JavaScript's every method to check whether all array objects meet a required condition.
How it connects
Contrasts every with some and filter as different ways to test or select array items.

Also explained in

Compare how 5 books explain all →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Secrets of the JavaScript Ninja, Third Edition. The section links point to the parts of the book it draws on.