liveBook · Manning

All

As explained in Secrets of the JavaScript Ninja, Second Edition

An Array method that returns true if every element passes a callback test.

Use every to check whether all items in an array satisfy a condition supplied by a callback. It returns true only when each item passes. section 9.1.4

Check every item

const allNinjasAreArmed = ninjas.every(ninja => "weapon" in ninja); This is false if even one ninja lacks a weapon. section 9.1.4

Listing 9.8. Testing arrays with the every and some methods
const ninjas = [
  {name: "Yagyu", weapon: "shuriken"},
  {name: "Yoshi" },
  {name: "Kuma", weapon: "wakizashi"}
];

const allNinjasAreNamed = ninjas.every(ninja => "name" in ninja);       #A
const allNinjasAreArmed = ninjas.every(ninja => "weapon" in ninja);     #A

assert(allNinjasAreNamed, "Every ninja has a name");
assert(!allNinjasAreArmed, "But not every ninja is armed");

const someNinjasAreArmed = ninjas.some(ninja => "weapon" in ninja);     #B
assert(someNinjasAreArmed, "But some ninjas are armed");                #B

Compare every, which checks all items, with some, which checks whether at least one item passes. section 9.1.4

How Secrets of the JavaScript Ninja, Second Edition covers it

How deep it goes
Shows callback-based condition checks with examples that produce true and false results.
Examples
Tests whether ninja objects all have names or weapons.
What you can do after
Apply Array.prototype.every to test whether each array item meets a condition.
How it connects
Contrasts every with some as array methods that test callback conditions.

Also explained in

Compare how 4 books explain all →

Related concepts

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