Sub topic 12
Finding Extremes and Magnitudes: min, max, abs, and sign
This chapter assumes you know how the Math object is organized and that you understand NaN, Infinity, and -0 from earlier chapters. You should also be comfortable with the spread syntax (...).
The working model
Math.max and Math.min take any number of separate arguments. They do not take an array. Each argument is converted to a number first, and the function returns the largest or smallest result. Math.abs returns a value's distance from zero. Math.sign tells you whether a value is positive, negative, or zero.
console.log(Math.max(3, 7, 2)); // 7
console.log(Math.min(...[3, 7, 2])); // 2
console.log(Math.max()); // -Infinity
console.log(Math.max(1, NaN)); // NaN
console.log(Math.abs(-5)); // 5
console.log(Math.sign(-5)); // -1Why the empty cases return Infinity
Math.max() returns -Infinity because that value is the identity for "largest so far". Any real number you compare against it wins. Math.min() returns Infinity for the same reason. This design lets you fold values one at a time without special-casing the start. It also means an empty array spreads into a result that looks wrong.
const scores = [];
const best = Math.max(...scores);
console.log(best); // -Infinity
console.log(Number.isFinite(best)); // false: treat as "no data"Common errors
Passing an array directly
Math.max([3, 7]) converts the array to a number. Here that conversion gives NaN, so the call returns NaN. Spread the array instead, or use reduce.
NaN poisons the result
A single NaN makes the whole result NaN. This often comes from an undefined entry or a string that fails to parse. Filter the input first.
Silent coercion
null converts to 0 and numeric strings convert to numbers. Neither case raises an error:
console.log(Math.max(null, -1)); // 0
console.log(Math.max("10", 2)); // 10
console.log(Math.max(undefined, 1)); // NaN
console.log(Math.abs("-3")); // 3
console.log(Math.abs([])); // 0Very large arrays
The spread syntax passes every element as a separate argument. Engines limit how many arguments a call can take, so spreading hundreds of thousands of elements can throw a RangeError. Use reduce for large or unbounded data:
const big = Array.from({ length: 500000 }, (_, i) => i);
const maxValue = big.reduce((m, v) => (v > m ? v : m), -Infinity);
console.log(maxValue); // 499999Signs and negative zero
Math.sign returns one of five values: 1, -1, 0, -0, or NaN. It keeps the sign of zero, and Math.abs removes it. Math.max treats 0 as larger than -0.
console.log(Object.is(Math.sign(-0), -0)); // true
console.log(Object.is(Math.abs(-0), 0)); // true
console.log(Object.is(Math.max(-0, 0), 0)); // true
console.log(Object.is(Math.min(-0, 0), -0)); // truePractical patterns
Combine min and max to clamp a value into a range. Use abs to measure the distance between two values without caring which one is larger.
function clamp(value, lo, hi) {
return Math.min(Math.max(value, lo), hi);
}
console.log(clamp(150, 0, 100)); // 100
console.log(clamp(-5, 0, 100)); // 0
console.log(Math.abs(3 - 10)); // 7Exercise
What do Math.min(...[]) and Math.max(4, "x") return, and why?
Show answer
Math.min(...[]) returns Infinity, because it was called with no arguments and Infinity is the starting value for a minimum. Math.max(4, "x") returns NaN, because "x" converts to NaN and any NaN argument makes the result NaN.
Exercise
Write a function range(values) that returns the largest value minus the smallest, returns 0 for an empty array, and ignores entries that are not finite numbers.
Show answer
function range(values) { const nums = values.filter(Number.isFinite); if (nums.length === 0) return 0; return Math.max(...nums) - Math.min(...nums); } For example, range([4, NaN, 9, "2", 1]) returns 8. Number.isFinite does not coerce, so it removes NaN and the string "2".