Open In App

Lodash _.negate() Method

Last Updated : 03 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

Lodash _.negate() method is used to create a function that negates the result of the given predicate function.

Syntax:

_.negate( predicate );

Parameters:

  • predicate: This parameter holds the predicate function to negate.

Return Value:

  • This method returns the new negated function.

Example 1: In this example, we are getting those values that are not divisible by 5 while using the function which is returning the value that is divisible by 5. It is just because of the use of the lodash _.negate() method.

JavaScript
// Requiring the lodash library  
const _ = require("lodash");

// Function to check the number
// is divisible by 5 or not
function number(n) {
    return n % 5 == 0;
}

// Using the _.negate() method  
console.log(
    _.filter([4, 6, 10, 15, 18],
        _.negate(number))
);

Output:

[4, 6, 18]

Example 2: In this example, we are getting those values that are not odd while using the function which is returning the odd numbers. It is just because of the use of the lodash _.negate() method.

JavaScript
// Requiring the lodash library  
const _ = require("lodash");

// Function to check the
// number is odd or not
function isOdd(n) {
    return n % 2 != 0;
}

// Using the _.negate() method  
console.log(
    _.filter([2, 4, 7, 12, 16, 19],
        _.negate(isOdd))
);

Output:

[2, 4, 12, 16]

Similar Reads