Open In App

Lodash _.mod() 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 _.mod() method is used to return the modulus of the given numbers, that is, the remainder of dividing the given dividend by the given divisor.



Syntax:

_.mod( dividend, divisor )

Parameters: This method takes two parameters as mentioned above and described below:



Return Value: This method returns the calculated modulus from the given arguments.

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 _.mod() method
var mod  = _.mod( 33, 5 ); 
    
console.log("The Modulus is :", mod);

Output:

The Modulus is : 3

Example 2:




// Defining lodash contrib variable 
var _ = require('lodash-contrib'); 
  
// Using the _.mod() method
var mod  = _.mod( 170, 35 ); 
    
console.log("The Modulus is :", mod);

Output:

The Modulus is : 30

Example 3:




// Defining lodash contrib variable 
var _ = require('lodash-contrib'); 
  
// Using the _.mod() method
var mod  = _.mod( 12, 1 ); 
    
console.log("The Modulus is :", mod);

Output:

The Modulus is : 0

Article Tags :