Open In App

Lodash _.neg() Method

Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc.

The _.neg() method is used to return a new number with the opposite sign value of the given number.



Syntax:

_.neg( value )

Parameters: This method accepts a single parameter as mentioned above and described below:



Return Value: This method returns a new number with the opposite sign value of the given number.

Note: This will not work in normal JavaScript because it requires the lodash contrib library to be installed. The Lodash contrib library can be installed using npm install lodash-contrib –save.

Example 1:




// Defining lodash contrib variable 
var _ = require('lodash-contrib'); 
  
// Using the _.neg() method
var n  = _.neg( -1 ); 
    
console.log(
  "Opposite sign value number is :", n
);

Output:

Opposite sign value number is : 1

Example 2:




// Defining lodash contrib variable 
var _ = require('lodash-contrib'); 
  
// Using the _.neg() method
var n  = _.neg( 1 ); 
    
console.log(
  "Opposite sign value number is :", n
);

Output:

Opposite sign value number is : -1

Example 3:




// Defining lodash contrib variable 
var _ = require('lodash-contrib'); 
  
// Using the _.neg() method
var n  = _.neg( 60 ); 
    
console.log(
  "Opposite sign value number is :", n
);

Output:

Opposite sign value number is : -60

Example 4:




// Defining lodash contrib variable 
var _ = require('lodash-contrib'); 
  
// Using the _.neg() method
var n  = _.neg( -50 ); 
    
console.log(
  "Opposite sign value number is :", n
);

Output:

Opposite sign value number is : 50


Article Tags :