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:
- value: It is the number to be negated with this method.
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:
Javascript
// 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:
Javascript
// 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:
Javascript
// 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:
Javascript
// 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
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, zambiatek 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!



