Course contentsShow
← JavaScript
Lesson 470 of 2,72312. Higher-Order FunctionsFree lesson

Some with Predicate Functions

Use some() to check for the existence of elements matching specific criteria in an array.

Some with Predicate Functions

What you'll learn: How to use predicate functions with some() to check if any element in an array meets custom conditions.

Understanding Predicate Functions with Some

A predicate function is simply a function that returns true or false based on some test. When you pass a predicate to some(), you're creating flexible, reusable logic for checking array elements.

Instead of writing simple inline checks, predicate functions let you:

  • Extract complex logic into named, testable functions
  • Reuse the same condition across different arrays
  • Make your code more readable and maintainable

Predicate Functions in Action

// Define reusable predicate functions
const isEven = num => num % 2 === 0;
const isLongString = str => str.length > 5;
const isAdult = person => person.age >= 18;

// Use with some()
const numbers = [1, 3, 7, 8, 9];
const hasEvenNumber = numbers.some(isEven); // true (8 is even)

const words = ['hi', 'cat', 'elephant'];
const hasLongWord = words.some(isLongString); // true ('elephant')

const users = [
  { name: 'Tim', age: 15 },
  { name: 'Sara', age: 22 }
];
const hasAdults = users.some(isAdult); // true (Sara is 22)

Combining Conditions

You can create more sophisticated predicates by combining conditions:

const isPositiveEven = num => num > 0 && num % 2 === 0;
const isValidUser = user => user.age >= 18 && user.name;

const nums = [-2, -4, 6];
nums.some(isPositiveEven); // true (6 matches)

Key Takeaway: Predicate functions with some() make your intent crystal clear and let you reuse test logic across your codebase, turning complex conditions into readable, named checks.