Math.max Of An Empty Spread Is -Infinity
Math.max and Math.min with an empty spread return -Infinity and Infinity, and a single NaN poisons the whole aggregate.
Code
// Spread of an empty array gives Math.max/min no arguments at all
console.log('max of empty:', Math.max(...[]));
console.log('min of empty:', Math.min(...[]));
// With values, spread replaces the old apply(this, arr) idiom
const scores = [12, 98, 45];
console.log('best:', Math.max(...scores));
console.log('worst:', Math.min(...scores));
// A single NaN poisons the whole result
console.log('with NaN:', Math.max(...scores, NaN));
// -Infinity is the identity for max: safe seed when reducing by hand
const best = scores.reduce((acc, n) => Math.max(acc, n), -Infinity);
console.log('reduced best:', best);Output
Captured from a sandboxed run of this snippet in the build pipeline.
max of empty: -Infinity
min of empty: Infinity
best: 98
worst: 12
with NaN: NaN
reduced best: 98What the code does
The file’s state is a handful of top-level bindings: scores, best.
Points of care
- Nothing to vet in a supply-chain review — the file pulls in no packages.
Using it
The file announces its steps in the output: max of empty:, min of empty:, best:.
Key statements worth quoting
const best = scores.reduce((acc, n) => Math.max(acc, n), -Infinity);
Interactive Tools for This Topic
GCD & LCM CalculatorFind the greatest common divisor and least common multiple of two or more integers, computed exactly with BigInt.SRI Hash GeneratorGenerate Subresource Integrity (SRI) hashes from resource content using SHA-256, SHA-384, or SHA-512, and copy a ready-to-use script tag.Vector Similarity CalculatorCompute Cosine Similarity, Dot Product, Euclidean L2, Manhattan L1, and Chebyshev distances between numeric vectors with dimension checks.
FAQ
- What has to be installed first?
- Nothing — the runtime itself provides everything the code touches. Its state lives in module-level bindings: `scores`, `best`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.