How to negate a predicate function in JavaScript?


In JavaScript, a predicate function is a function that returns a Boolean value. In other words, it is a function that tests whether a certain condition is true or false.

There are times when we need to negate a predicate function. That is, we need to return the opposite Boolean value.

There are several ways to negate a predicate function in JavaScript.

Using the ! operator

The most common way to negate a predicate function is to use the ! operator.

For example, consider the following predicate function −

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

To negate this function, we can use the ! operator as follows −

function isOdd(num) {
   return !isEven(num);
}

The isOdd() function above returns the opposite Boolean value of isEven().

Using the !! operator

Another way to negate a predicate function is to use the !! operator.

The !! operator converts a value to a Boolean value. If the value is already a Boolean value, it is simply returned.

For example, consider the following predicate function −

function isTruthy(value) {
   return !!value;
}

To negate this function, we can use the !! operator as follows −

function isFalsy(value) {
   return !!isTruthy(value);
}

The isFalsy() function above returns the opposite Boolean value of isTruthy().

Using the Boolean() function

Another way to negate a predicate function is to use the Boolean() function.

The Boolean() function converts a value to a Boolean value. If the value is already a Boolean value, it is simply returned.

For example, consider the following predicate function −

function isTruthy(value) {
   return Boolean(value);
}

To negate this function, we can use the Boolean() function as follows −

function isFalsy(value) {
   return Boolean(isTruthy(value));
}

The isFalsy() function above returns the opposite Boolean value of isTruthy().

There are several benefits of negating predicate functions −

First, it can make our code more readable. Consider the following example −

function isUserValid(user) {
   return !isAdmin(user);
}

The isUserValid() function above is more readable than the following equivalent function −

function isUserValid(user) {
   return isAdmin(user) === false;
}

Second, negating predicate functions can help us avoid errors. Consider the following example −

function isUserValid(user) {
   return isAdmin(user);
}

If we forget to negate the isAdmin() function, the isUserValid() function will always return true, regardless of the user argument.

Third, negating predicate functions can help us write more concise code. Consider the following example −

function isUserValid(user) {
   return !isAdmin(user) && isActive(user);
}

The isUserValid() function above is more concise than the following equivalent function −

function isUserValid(user) {
   return isAdmin(user) === false && isActive(user) === true;
}

In conclusion, there are several ways to negate a predicate function in JavaScript. The most common way is to use the ! operator.

Other ways include using the !! operator or the Boolean() function.

Updated on: 04-Aug-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements