Sunday, December 29, 2024
Google search engine
HomeLanguagesJavascriptLodash _.complement() Method

Lodash _.complement() Method

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

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

Recent Comments