JavaScript Math.max() and Math.min()

Math.max() returns the largest of its arguments and Math.min() returns the smallest. They accept separate numeric arguments, so arrays are usually expanded with the spread syntax.

console.log(Math.max(3, 12, 4, 14)); // 14
console.log(Math.min(5, 6, -10, 2)); // -10

Math.max() and Math.min() syntax Top ↑

Math.max(value1, value2, ...moreValues)
Math.min(value1, value2, ...moreValues)

Finding the maximum value Top ↑

console.log(Math.max(3, 12, 4, 14, 7, 5)); // 14
console.log(Math.max(-5, 6, -22, 2, -20)); // 6
console.log(Math.max(-12, -23, -3)); // -3

Finding the minimum value Top ↑

console.log(Math.min(5, 6, 2, 20, 8)); // 2
console.log(Math.min(5, 6, -10, 2, 20, -6)); // -10
console.log(Math.min(-8, -5, -2)); // -8

Finding min or max in an array Top ↑

const values = [8, 3, 19, -4];
console.log(Math.max(...values)); // 19
console.log(Math.min(...values)); // -4

Invalid values and NaN Top ↑

console.log(Math.max(4, "9"));   // 9
console.log(Math.max(4, "nine")); // NaN

What happens with no arguments? Top ↑

console.log(Math.max()); // -Infinity
console.log(Math.min()); // Infinity

Very large arrays Top ↑

The spread approach is convenient for ordinary arrays, but spreading extremely large arrays into a function call can exceed an engine's argument limit. For very large data sets, reduce the array instead.

const values = [8, 3, 19, -4];
const largest = values.reduce((a, b) => a > b ? a : b);
console.log(largest); // 19

Math.abs()




Subscribe to our YouTube Channel here



plus2net.com










✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer