Negate a Predicate Function in JavaScript

Last Updated : 22 Aug, 2026

A predicate function returns a Boolean value based on a condition. You can negate its result using the ! operator or create a reusable function that returns the opposite result.

  • Use the ! operator to directly reverse a predicate's result.
  • Create a negation function to reuse the same logic with different predicates.
  • This avoids duplicating predicate conditions and improves code reusability.

Approach 1: Using Direct Negation

The simplest way to negate a predicate is to use the ! operator on its result.

JavaScript
function isOdd(number) {
    return number % 2 === 1;
}

function isEven(number) {
    return number % 2 === 0;
}

console.log(isOdd(5));
console.log(!isOdd(5));

console.log(isEven(4));
console.log(!isEven(4));

Output
true
false
true
false

Here, !isOdd(5) returns the opposite of isOdd(5).

Approach 2: Creating a Negated Predicate

Instead of repeating the ! operator wherever needed, you can create a separate function that returns the opposite result of a predicate.

JavaScript
function isOdd(number) {
    return number % 2 === 1;
}

function isNotOdd(number) {
    return !isOdd(number);
}

console.log(isOdd(5));
console.log(isNotOdd(5));

Output
true
false

Similarly, another predicate can be negated:

JavaScript
function isEven(number) {
    return number % 2 === 0;
}

function isNotEven(number) {
    return !isEven(number);
}

console.log(isEven(4));
console.log(isNotEven(4));

Output
true
false

Approach 3: Using a Reusable Negate Function

A generic negate() function can accept any predicate and return a new function that produces the opposite Boolean result.

JavaScript
function isOdd(number) {
    return number % 2 === 1;
}

function isEven(number) {
    return number % 2 === 0;
}

function negate(predicate) {
    return function (number) {
        return !predicate(number);
    };
}

const isNotOdd = negate(isOdd);
const isNotEven = negate(isEven);

console.log(isOdd(5));
console.log(isNotOdd(5));

console.log(isEven(4));
console.log(isNotEven(4));

Output
true
false
true
false

This approach is more reusable because the same negate() function can work with any predicate function.

Comment