01 / 21

find / some / every

Master three essential array methods that search, test, and validate your data using callback functions.

AP Computer Science JavaScript

What Is a Callback?

A callback is a function you pass as an argument to another function. The receiving function "calls back" your function at the right moment: once per element in the array.

// a callback is just a regular function function isEven(num) { return num % 2 === 0; } // we pass it to an array method [1, 2, 3, 4].find(isEven); // 2

Three Methods, Three Questions

.find()
"Which element matches?" Returns the first matching value or undefined.
.some()
"Does at least one match?" Returns true or false.
.every()
"Do all of them match?" Returns true or false.

How the Callback Flows

3 7 8 2 10 skipped n => n > 5 false true .find() returns 7 Stops at first true: no need to check remaining elements

.find(): Return the First Match

const nums = [10, 23, 42, 55, 60]; const result = nums.find(function(n) { return n > 40; }); console.log(result); // 42
1
Iterates through each element and calls your callback.
2
The first time the callback returns true, that element value is returned.
3
If no element matches, returns undefined.

.some(): Does Any Match?

const ages = [12, 16, 21, 14]; const hasAdult = ages.some(age => age >= 18); console.log(hasAdult); // true
Returns Boolean
true if at least one element passes the test.
Short-circuits
Stops as soon as one true is found: never visits remaining elements.

.every(): Do All Match?

const scores = [85, 92, 78, 95]; const allPassed = scores.every(s => s >= 70); console.log(allPassed); // true
Returns Boolean
true only if every single element passes the test.
Short-circuits
Stops as soon as one false is found: one failure means the answer is false.

Key Distinction: What They Return

.find() .some() .every()
Returns Element or undefined Boolean Boolean
Question it answers "Which one?" "Is there any?" "Are they all?"
Short-circuits on First true First true First false
Empty array result undefined false true

How to Apply: Step by Step

1
Identify your question. Do you need the value itself, or just a yes/no answer?
2
Choose the method. Need the element? Use .find(). Need "any?" Use .some(). Need "all?" Use .every().
3
Write your callback. It receives (element, index, array) and must return true or false.
4
Handle the result. For .find(), check for undefined. For .some()/.every(), use the boolean directly in conditionals.

Example 1: Finding a User

const users = [ { name: "Alice", age: 17 }, { name: "Bob", age: 22 }, { name: "Carol", age: 19 } ]; const firstAdult = users.find(u => u.age >= 18); console.log(firstAdult); // { name: "Bob", age: 22 }

Example 2: Validating a Form

const fields = ["Alice", "alice@mail.com", "", "abc123"]; // Are ALL fields filled in? const allFilled = fields.every(f => f.length > 0); console.log(allFilled); // false // Is there any empty field? const hasEmpty = fields.some(f => f.length === 0); console.log(hasEmpty); // true

Example 3: Combining Methods

const orders = [ { id: 1, items: ["book", "pen"], paid: true }, { id: 2, items: ["laptop"], paid: false }, { id: 3, items: ["phone", "case"], paid: true } ]; // Find the unpaid order const unpaid = orders.find(o => !o.paid); // { id: 2, items: ["laptop"], paid: false } // Are all orders paid? const allPaid = orders.every(o => o.paid); // false // Does any order include "laptop"? const hasLaptop = orders.some( o => o.items.includes("laptop") ); // true

Arrow Function Shorthand

All three methods work with any function, but arrow functions make the code concise. When the body is a single expression, you can omit the braces and return:

// Verbose nums.find(function(n) { return n > 10; }); // Concise arrow nums.find(n => n > 10);

Callback Parameters: Beyond Element

const letters = ["a", "b", "c", "d"]; // The callback receives up to 3 arguments: letters.find((element, index, array) => { console.log(element, index, array); return index === 2; }); // "a" 0 ["a","b","c","d"] // "b" 1 ["a","b","c","d"] // "c" 2 ["a","b","c","d"] <-- match! returns "c"
element
The current item being tested.
index
Its position (0-based) in the array.
array
The original array itself (rarely used).

Common Mistakes

✕ Forgetting the return
With braces { }, you must write return. Without it, the callback returns undefined (falsy), so nothing matches.
✕ Using .find() for a boolean
.find() returns the element, not true/false. If the first match is 0 or "", it's falsy even though it matched.
✕ Confusing .some() with .filter()
.some() returns a boolean. .filter() returns a new array of all matches. Pick the right tool.

Summary Comparison

.find() .some() .every()
Purpose Get the first match Check if any match Check if all match
Return type Element or undefined Boolean Boolean
Stops early? On first true On first true On first false
Mutates array? No No No
Logic analogy First match OR AND

Question 1 of 5

What does [5, 12, 8, 1].find(n => n > 10) return?

Question 2 of 5

What does [3, 5, 7].every(n => n % 2 !== 0) return?

Question 3 of 5

What does [].some(x => x > 0) return?

Question 4 of 5

What does result equal?

Question 5 of 5

Which method should you use to check if every student scored above 60?