Skip to content

Instantly share code, notes, and snippets.

@pablocattaneo
Created March 15, 2026 13:40
Show Gist options
  • Select an option

  • Save pablocattaneo/2c0b7e986142b67a14013387e947f6ba to your computer and use it in GitHub Desktop.

Select an option

Save pablocattaneo/2c0b7e986142b67a14013387e947f6ba to your computer and use it in GitHub Desktop.

A predicate function is a function that returns a boolean value—that is, it returns true or false depending on whether a condition is satisfied.

In other words, a predicate tests something.

Simple example (JavaScript)

function isEven(number) {
  return number % 2 === 0;
}
  • Input: a number
  • Output: true if the number is even, otherwise false

Usage:

isEven(4); // true
isEven(5); // false

Example used with array methods

Predicate functions are very common in functions like .filter(), .find(), .some(), etc.

const numbers = [1, 2, 3, 4, 5];

const evenNumbers = numbers.filter((n) => n % 2 === 0);

Here:

(n) => n % 2 === 0

is the predicate function. For each element, it answers the question: “Does this element match the condition?”

Another example

const isAdult = (person) => person.age >= 18;
  • Returns true → person is an adult
  • Returns false → person is not

Simple mental model

A predicate function = a yes/no question in code.

Examples:

  • isEven(n)
  • isEmpty(str)
  • hasPermission(user)
  • isLoggedIn(session)

All of them evaluate a condition and return true or false.


Since you work with React Native and TypeScript, you'll see predicate functions a lot with things like:

items.find(item => item.id === targetId)

Here the predicate is:

item => item.id === targetId

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment