- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
- Related Articles
- negate function in C++ STL
- How to test a value x against predicate function and returns fn(x) or x in JavaScript?
- Difference between Function and Predicate in Java 8
- Negate a BigInteger in Java
- Negate a BigDecimal value in Java
- How to call a function in JavaScript?
- How to define a function in JavaScript?
- How to invoke a function with a function constructor in JavaScript?
- How to invoke a JavaScript Function as a function?
- How to call a function that returns another function in JavaScript?
- How to call a JavaScript function in HTML?
- How to define a JavaScript function using Function() Constructor?
- How to call a Java function inside JavaScript Function?
- How to convert a string in to a function in JavaScript?
- How to use Predicate and BiPredicate in lambda expression in Java?
