Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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 (Logical NOT)
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;
}
function isOdd(num) {
return !isEven(num);
}
// Test the functions
console.log(isEven(4)); // true
console.log(isOdd(4)); // false
console.log(isEven(5)); // false
console.log(isOdd(5)); // true
true false false true
The isOdd() function above returns the opposite Boolean value of isEven().
Using Higher-Order Functions to Create Negated Predicates
A more functional approach is to create a higher-order function that negates any predicate:
function not(predicate) {
return function(...args) {
return !predicate(...args);
};
}
// Create predicates
const isEven = (num) => num % 2 === 0;
const isPositive = (num) => num > 0;
// Create negated predicates
const isOdd = not(isEven);
const isNotPositive = not(isPositive);
console.log(isEven(6)); // true
console.log(isOdd(6)); // false
console.log(isPositive(-3)); // false
console.log(isNotPositive(-3)); // true
true false false true
Array Methods with Negated Predicates
Negated predicates are commonly used with array methods like filter(), find(), and some():
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const isEven = (num) => num % 2 === 0;
const isOdd = (num) => !isEven(num);
// Filter even and odd numbers
const evenNumbers = numbers.filter(isEven);
const oddNumbers = numbers.filter(isOdd);
console.log("Even numbers:", evenNumbers);
console.log("Odd numbers:", oddNumbers);
Even numbers: [ 2, 4, 6, 8, 10 ] Odd numbers: [ 1, 3, 5, 7, 9 ]
Common Mistake: Using !! for Negation
The !! operator converts values to Boolean but doesn't negate them. It's often misunderstood:
function isTruthy(value) {
return !!value; // Converts to Boolean
}
function isFalsy(value) {
return !value; // Negates the value (correct)
}
// Incorrect way (doesn't negate)
function incorrectFalsy(value) {
return !!isTruthy(value); // This doesn't negate!
}
console.log(isTruthy("hello")); // true
console.log(isFalsy("hello")); // false
console.log(incorrectFalsy("hello")); // true (not negated!)
true false true
Benefits of Negating Predicate Functions
There are several benefits of negating predicate functions:
Improved Readability: Makes code more expressive and self-documenting:
function isRegularUser(user) {
return !isAdmin(user); // More readable
}
function isAdmin(user) {
return user.role === 'admin';
}
// Test the functions
const user1 = { name: "John", role: "user" };
const user2 = { name: "Jane", role: "admin" };
console.log(isRegularUser(user1)); // true
console.log(isRegularUser(user2)); // false
true false
Concise Code: Eliminates verbose comparisons:
// Concise with negation
const activeNonAdmins = users => users.filter(user => !isAdmin(user) && isActive(user));
// Verbose without negation
const activeNonAdminsVerbose = users => users.filter(user =>
isAdmin(user) === false && isActive(user) === true
);
function isAdmin(user) { return user.role === 'admin'; }
function isActive(user) { return user.status === 'active'; }
const users = [
{ name: "Alice", role: "user", status: "active" },
{ name: "Bob", role: "admin", status: "active" }
];
console.log(activeNonAdmins(users));
[ { name: 'Alice', role: 'user', status: 'active' } ]
Conclusion
The most straightforward way to negate a predicate function is using the ! operator. Higher-order functions provide reusable negation patterns for functional programming approaches.
