Syntax, Parameter and Return Value
Syntax:
_.min(array)
Parameters:
array (Array): The array to iterate over.
Return Value:
(Number) - Returns the minimum value found in the array. If the array is empty or falsey, _.min() returns undefined.
Examples
Basic Usage to Find Minimum Value:
JavaScript
var _ = require('lodash');
console.log(_.min([4, 2, 8, 6]));

You can also try this code with Online Javascript Compiler
Run Code
Output:
2
Demonstrates finding the smallest number in a simple array.
Handling Empty Array:
JavaScript
console.log(_.min([]));

You can also try this code with Online Javascript Compiler
Run Code
Output:
undefined
Shows that _.min() returns undefined when applied to an empty array.
Application in Data Analysis:
JavaScript
var temperatures = [76, 85, 90, 68, 88];
var lowestTemperature = _.min(temperatures);
console.log('Lowest Temperature:', lowestTemperature);

You can also try this code with Online Javascript Compiler
Run Code
Output:
'Lowest Temperature: 68'
An example of using _.min() to find the lowest temperature in a collection of data.
Comparing with Native JavaScript Methods:
JavaScript
var numbers = [10, 21, 3, 15];
var minNumber = Math.min(...numbers);
console.log(minNumber);

You can also try this code with Online Javascript Compiler
Run Code
Output:
3
Demonstrates achieving a similar result with native JavaScript's Math.min() for comparison.
Frequently Asked Questions
Does _.min() work with non-numeric values?
_.min() is designed for arrays of numbers. Non-numeric values in the array are ignored in the calculation, and may lead to undefined if no numeric values are present.
How does _.min() handle NaN or undefined values in the array?
NaN or undefined values in the array are ignored by _.min(), and they do not affect the calculation of the minimum value.
Can _.min() be used with arrays of objects?
To find the minimum value in an array of objects based on a specific property, you should use _.minBy() instead, which allows specifying an iteratee for comparison.
Conclusion
Lodash's _.min() method provides a simple and efficient way to find the minimum value in an array of numbers. It enhances code readability and is especially useful in comparison to more verbose native JavaScript approaches in certain scenarios.
You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.