HomeLanguagesJavascriptLodash _.negate() Method

Lodash _.negate() Method

Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc.

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

Syntax:

_.negate( predicate )

Parameters: This method accepts a single parameter as mentioned above and described below:

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

Return Value: This method returns the new negated function.

Example 1:

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:  

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]
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32712 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7086 POSTS0 COMMENTS
Nicole Veronica
12225 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12309 POSTS0 COMMENTS
Shaida Kate Naidoo
7202 POSTS0 COMMENTS
Ted Musemwa
7468 POSTS0 COMMENTS
Thapelo Manthata
7187 POSTS0 COMMENTS
Umr Jansen
7171 POSTS0 COMMENTS