Lodash - Math
Lodash - Math Operations
Section titled “Lodash - Math Operations”Lodash includes several functions for performing common mathematical operations. While JavaScript’s built-in Math object provides core functionalities, Lodash often adds convenience, especially when working with collections of numbers or requiring specific precision.
Key Math Methods
Section titled “Key Math Methods”| Method & Syntax | Description & Comparison to Native JS |
|---|---|
| .add(augend, addend) .subtract(minuend, subtrahend) .multiply(multiplier, multiplicand) .divide(dividend, divisor) | Basic arithmetic operations: addition, subtraction, multiplication, division. Functionally similar to native +, -, *, / operators, but provided as functions for potential use in functional composition (e.g., with _.reduce). |
| .ceil(number, [precision=0]) .floor(number, [precision=0]) _.round(number, [precision=0]) | Rounds number up (ceil), down (floor), or to the nearest integer/precision. Unlike Math.ceil/floor/round, these methods accept an optional precision argument to round to a specific decimal place (positive precision) or power of 10 (negative precision). |
| .max(array) .min(array) | Computes the maximum or minimum value in an array. If the array is empty or falsey, undefined is returned. Native Math.max(...array) and Math.min(...array) achieve similar results but may fail on very large arrays due to argument limits. |
| .maxBy(array, [iteratee=.identity]) .minBy(array, [iteratee=.identity]) | Finds the element in array that yields the maximum or minimum value after applying the iteratee function to each element. Useful for finding the object with the highest/lowest property value. Native JS requires sorting or manual iteration. |
| .mean(array) .meanBy(array, [iteratee=_.identity]) | Computes the average of the values in array. meanBy applies iteratee to each element before calculating the average. No direct native equivalent. |
| .sum(array) .sumBy(array, [iteratee=_.identity]) | Computes the sum of the values in array. sumBy applies iteratee to each element before summing. While sum can be achieved with native reduce, _.sum/sumBy are often more concise. |
Example: Precision Rounding
Section titled “Example: Precision Rounding”_.ceil(4.006); // => 5 (like Math.ceil)_.ceil(6.004, 2); // => 6.01 (round up to 2 decimal places)_.ceil(6040, -2); // => 6100 (round up to nearest 100)
_.floor(4.006); // => 4 (like Math.floor)_.floor(0.046, 2); // => 0.04_.floor(4060, -2); // => 4000
_.round(4.006); // => 4 (like Math.round)_.round(4.006, 2); // => 4.01_.round(4060, -2); // => 4100Example: Using maxBy and sumBy
Section titled “Example: Using maxBy and sumBy”const products = [ { name: 'apple', price: 1.2 }, { name: 'banana', price: 0.8 }, { name: 'orange', price: 1.5 }];
const mostExpensive = _.maxBy(products, 'price');// => { name: 'orange', price: 1.5 }
const totalValue = _.sumBy(products, 'price');// => 3.5Lodash’s math functions shine when dealing with collections or requiring specific rounding precision, offering cleaner alternatives to potentially complex native implementations.
Find the complete list at the official Lodash Math documentation.