The Lodash _.complement() method returns a function that reverses the sense of a given predicate-function.
Syntax:
_.complement( function );
Parameters: This method accepts a single parameter as listed above and discussed below.
- function: This parameter predicate function defined containing the returning logic.
Return Value: This method returns a function.
Note: To execute the below examples, you have to install the lodash-contrib library by using this command prompt and execute the following command.
npm install lodash-contrib
Below examples illustrate the Lodash _.complement() method in JavaScript:
Example 1:
Javascript
// Defining lodash contrib variable var _ = require('lodash-contrib');Â Â Â function gfgFun (x) { Â Â Â Â return x>=2 ; } Â Â var comp = _.complement(gfgFun); Â Â var x=1000; Â Â console.log("Without Complement Function:",gfgFun(x)) Â Â console.log("With Complement Function:",comp(x)); |
Output:
Without Complement Function: true With Complement Function: false
Example 2:Â
Javascript
// Defining lodash contrib variable var _ = require('lodash-contrib');Â Â Â function gfgFun (x) { Â Â Â Â return x=="Geeks" ; } Â Â var comp = _.complement(gfgFun); Â Â var x="neveropen"; console.log("Without Complement Function:",gfgFun(x)) Â Â console.log("With Complement Function:",comp(x)); |
Output:
Without Complement Function: false With Complement Function: true
