Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Why This Function is Used
3.
Syntax, Parameter and Return Value
3.1.
Syntax: 
3.2.
Parameters:
3.3.
Return Value:
4.
Examples 
4.1.
Negating a Simple Predicate:
4.2.
JavaScript
4.3.
Using with Array Filtering:
4.4.
JavaScript
4.5.
Inverting Predicates for Array Rejection:
4.6.
JavaScript
4.7.
Combining with Other Functional Utilities:
4.8.
JavaScript
5.
Frequently Asked Questions 
5.1.
Can _.negate() be used with functions that take multiple arguments?
5.2.
Is _.negate() limited to use with lodash functions?
5.3.
How does _.negate() improve code readability?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.negate() Method

Author Ravi Khorwal
0 upvote
Roadmap to SDE career at Amazon
Speaker
Anubhav Sinha
SDE-2 @
25 Jun, 2024 @ 01:30 PM

Introduction

In programming, particularly in conditional logic and functional programming, the need to reverse the truthiness of predicate functions is a common occurrence. Lodash simplifies this with the _.negate() method. This function creates a negated version of a predicate function, meaning that it returns the opposite boolean value. 

Lodash _.negate() Method

This article will explore the _.negate() method, discussing its usefulness, syntax, and applications, complemented by examples and FAQs.

Why This Function is Used

The _.negate() function is used to invert the truthiness of a given predicate function. This is especially useful when working with higher-order functions like _.filter() or _.find(), where you might need to select items that do not match a certain condition. Instead of rewriting the predicate function, _.negate() provides a concise way to reverse its logic, enhancing code readability and maintainability.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Syntax, Parameter and Return Value

Syntax: 

_.negate(predicate)

Parameters:

predicate (Function): The predicate function to negate.

Return Value:

 (Function) - Returns the new negated function.

Examples 

Negating a Simple Predicate:

  • JavaScript

JavaScript

var _ = require('lodash');

function isEven(n) {

 return n % 2 === 0;

}

var isOdd = _.negate(isEven);

console.log(isOdd(2));

console.log(isOdd(3));

Output: 

false
true


Demonstrates creating a negated function to check for odd numbers.

Using with Array Filtering:

  • JavaScript

JavaScript

var numbers = [1, 2, 3, 4, 5];

var oddNumbers = _.filter(numbers, _.negate(isEven));

console.log(oddNumbers);

Output:

[1, 3, 5]


Shows using _.negate() in conjunction with _.filter() to select odd numbers.

Inverting Predicates for Array Rejection:

  • JavaScript

JavaScript

var names = ['Alice', 'Bob', 'Charlie', 'David'];

function startsWithA(name) {

 return name.startsWith('A');

}

var notStartingWithA = _.reject(names, _.negate(startsWithA));

console.log(notStartingWithA);

 Output: 

['Bob', 'Charlie', 'David']


An example of using _.negate() for rejecting array elements.

Combining with Other Functional Utilities:

  • JavaScript

JavaScript

var users = [{ name: 'Alice', active: true }, { name: 'Bob', active: false }];

var inactiveUsers = _.filter(users, _.negate('active'));

console.log(inactiveUsers);

 Output: 

[{ name: 'Bob', active: false }]


Demonstrates combining _.negate() with _.filter() for selecting objects based on a negated property.

Frequently Asked Questions 

Can _.negate() be used with functions that take multiple arguments?

Yes, _.negate() can be used with functions that take multiple arguments, but it negates only the boolean result of the function, not the arguments themselves.

Is _.negate() limited to use with lodash functions?

No, _.negate() can be used with any JavaScript function that returns a boolean value. It's not limited to lodash-specific functions.

How does _.negate() improve code readability?

_.negate() improves code readability by clearly expressing the intention to use the opposite logic of a predicate function, avoiding the need for more verbose or manual negations.

Conclusion

Lodash's _.negate() method is a straightforward and effective way to invert the result of predicate functions. It is particularly useful in scenarios involving conditional logic and functional programming patterns, allowing for cleaner and more readable code.

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Previous article
Lodash _.assignWith() Method
Next article
Lodash _.reject() Method
Live masterclass