A predicate function is simply a function that takes an input and returns a boolean value: exactly true or false.
Think of a predicate function like a bouncer at a club. You hand the bouncer an item, and the bouncer evaluates it and says either “Yes” (true) or “No” (false).
Why do we need them?
You have actually already used them! In JavaScript, predicate functions are mostly used as callbacks for array methods that need to test elements against a condition.
Methods like filter(), find(), and findIndex() all require a predicate function to work.
Example 1: A Standard Predicate Function
// This is a predicate function. It only returns true or false.
function isEven(number) {
return number % 2 === 0;
}
console.log(isEven(4)); // true
console.log(isEven(5)); // false
Example 2: Using it with Arrays
When you pass a predicate function to an array method, the method runs that “true/false” test on every single item.
const numbers = [1, 2, 3, 4, 5, 6]; // filter() uses the predicate to decide what to keep const evenNumbers = numbers.filter(isEven); console.log(evenNumbers); // [2, 4, 6]
The Modern “React” Way (Arrow Functions)
In modern JavaScript and React, you rarely write out a full function block for a predicate. Instead, you write it inline as a tiny, one-line Arrow Function.
const ages = [12, 18, 25, 8]; // The inline arrow function `age => age >= 18` is the predicate! const adults = ages.filter(age => age >= 18); console.log(adults); // [18, 25]