liveBook · Manning

Any

As explained in Secrets of the JavaScript Ninja, Second Edition

An Array method that returns true if at least one element passes a callback test.

Use some to test whether at least one array element passes a callback condition. It returns true when a match exists. 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

A ninja array checks whether any ninja has a weapon; the result is true because some do. section 9.1.4

How Secrets of the JavaScript Ninja, Second Edition covers it

How deep it goes
Defines the callback test with a concrete example, without detailing execution mechanics.
Examples
Checks whether any ninja object has a weapon property.
What you can do after
Apply JavaScript's Array some method to test whether any item meets a condition.
How it connects
Pairs some with every for testing array items against conditions.

Also explained in

Compare how 2 books explain any →

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.