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