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.

JavascriptData Integrity

Code

javascriptsnippet
15 lines
// 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: 98

What 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

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`.