Underscore.js is a JavaScript library that provides a lot of useful functions that help in the programming in a big way like the map, filter, invoke, etc even without using any built-in objects.
The _.negate() function is an inbuilt function in Underscore.js library of JavaScript which is used to find a new negated version of the stated predicate function.
Syntax:
_.negate(predicate)
Parameters: It accepts a single parameters which is specified below:
- predicate: It is the stated predicate function.
Return Value: This method returns a new negated version of the stated predicate function.
Example 1:
<!DOCTYPE html> <html> Â Â <head> Â Â Â Â <script src= Â Â Â Â </script> </head> Â Â <body> Â Â Â Â <script> Â Â Â Â Â Â Â Â var isNaN = _.negate(Boolean); Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â console.log(_.find( Â Â Â Â Â Â Â Â Â Â Â Â [3, -11, undefined, 41, 0], isNaN)); Â Â Â Â </script> </body> Â Â </html> |
Output:
undefined
Example 2:
<!DOCTYPE html> <html> Â Â <head> Â Â Â Â <script src= Â Â Â Â </script> </head> Â Â <body> Â Â Â Â <script> Â Â Â Â Â Â Â Â function isCSportal(character) { Â Â Â Â Â Â Â Â Â Â Â Â return character === "neveropen" ; Â Â Â Â Â Â Â Â } Â Â Â Â Â Â Â Â Â Â var isNotCSportal = _.negate(isCSportal); Â Â Â Â Â Â Â Â console.log(isNotCSportal( "neveropen" )); Â Â Â Â Â Â Â Â console.log(isNotCSportal( "GfG" )); Â Â Â Â </script> </body> Â Â </html> |
Output:
false true
Reference: https://underscorejs.org/#negate