The _.complement() method returns a function that reverses the sense of a given predicate function.
Syntax:
_.complement( function );
Parameters:
- function: predicate function defined containing the returning logic.
Return Value: This method returns a function.
Note: This will not work in normal JavaScript because it requires the underscore.js contrib library to be installed.
underscore.js contrib library can be installed using npm install underscore-contrib –save.
Example 1: In this example, we will see the use of the _.complement() Method
javascript
// Defining underscore contrib variable const _ = require( 'underscore-contrib' );
function gfgFun(x) {
return x >= 2;
} let comp = _.complement(gfgFun); let x = 3; 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: In this example, we will see the use of the _.complement() Method
javascript
// Defining underscore contrib variable const _ = require( 'underscore-contrib' );
function gfgFun(x) {
return x == "Geeks" ;
} let comp = _.complement(gfgFun); let x = "Geek" ;
console.log( "Without Complement Function:" , gfgFun(x))
console.log( "With Complement Function:" , comp(x));
|
Output:
Without Complement Function: false With Complement Function: true