Open In App

Lodash _.max() Method

Lodash _.max() method is used to find the maximum element from the array. If the array is empty then undefined is returned.

Syntax:

_.max( array );

Parameters:

Return Value:

This method returns the maximum element from the array.



Example 1: In this example, we are trying to find out the max element in an empty array and it is returning undefined.




// Requiring the lodash library 
const _ = require("lodash");
 
// Use of _.max() method
let max_val = _.max([]);
 
// Printing the output 
console.log(max_val);

Output:



undefined

Example 2: In this example, we are trying to find out the max element in an array by the use of the _.max() method.




// Requiring the lodash library 
const _ = require("lodash");
 
// Use of _.max() method
let max_val = _.max([15, 7, 38, 46, 82]);
 
// Printing the output 
console.log(max_val);

Output:

82

Example 3: In this example, we are trying to find out the max element in an array by the use of the _.max() method.




// Requiring the lodash library 
const _ = require("lodash");
 
// Use of _.max() method
let max_val = _.max([-15, 7, 38, -46, -82]);
 
// Printing the output 
console.log(max_val);

Output:

38

Article Tags :