Open source JavaScript npm

Maths.js

The numeric helpers the native Math object skipped. Statistical aggregates, number theory, clamping, and percentiles. Under 1 kb gzipped, zero dependencies.

The gap between Math and a full library.

The native Math object covers basic operations: rounding, trigonometry, powers. But statistical aggregates, percentile lookups, number theory basics like GCD and LCM, and safe clamping keep appearing in application work without native solutions.

The common workarounds are writing those helpers inline again and again, or pulling in a large library that brings far more surface area than needed. Maths.js sits between those extremes: small enough to drop anywhere, complete enough to cover the methods that actually come up, and strict enough to throw clear errors instead of silently returning NaN.

Core features

Small, strict, and self-contained.

Under 1 kb gzipped

Drop it into dashboards, data scripts, or static pages without adding meaningful weight to your bundle.

Strict input validation

Every method throws a typed TypeError or RangeError with a clear message instead of silently returning NaN.

Zero dependencies

Works in the browser via <script> tag or in Node.js via require(). No build step, no transitive packages.

npm, CDN, or clone.

npm

npm install @peterbenoit/mathsjs

CDN (jsDelivr)

<script src="https://cdn.jsdelivr.net/npm/@peterbenoit/mathsjs/Maths.js"\\></script>

Node.js

const Maths = require('@peterbenoit/mathsjs');

Maths.avg(10, 20, 30);  // 20
Maths.median(7, 1, 4);  // 4
Maths.gcd(48, 18);       // 6

Attaches to globalThis.Maths in browser contexts. No bundler or build step needed.

All static methods on Maths.

Aggregation

MethodDescriptionReturns
avg(...values)Arithmetic meannumber
sum(...values)Total of all valuesnumber

Statistics

MethodDescriptionReturns
median(...values)Middle value; mean of two middle for even-length inputsnumber
mode(...values)Most frequent value(s); null when all are uniquenumber[] | null
range(...values)Difference between max and minnumber
percentile(values, p)Value at the pth percentile (0–100)number

Number Theory

MethodDescriptionReturns
factorial(n)Product of all positive integers up to nnumber
gcd(a, b)Greatest common divisornumber
lcm(a, b)Least common multiplenumber

Utility

MethodDescriptionReturns
clamp(value, min, max)Constrains a value to the range [min, max]number
distance(x1, y1, x2, y2)Euclidean distance between two 2D pointsnumber

Common numeric patterns.

Statistics

Maths.avg(10, 20, 30);        // 20
Maths.median(1, 3, 2);       // 2
Maths.mode(1, 2, 2, 3);       // [2]
Maths.percentile([1,2,3,4,5], 90); // 5

Number theory

Maths.factorial(5);  // 120
Maths.gcd(48, 18);    // 6
Maths.lcm(4, 6);      // 12

Utility

Maths.clamp(12, 0, 10);          // 10
Maths.distance(0, 0, 3, 4);    // 5

The math helpers you keep writing by hand.

MIT licensed. Available on npm and jsDelivr. Built-in test suite using Node's native test runner.

Related paths.