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)); // -10Math.max(value1, value2, ...moreValues)
Math.min(value1, value2, ...moreValues)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)); // -3console.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)); // -8const values = [8, 3, 19, -4];
console.log(Math.max(...values)); // 19
console.log(Math.min(...values)); // -4console.log(Math.max(4, "9")); // 9
console.log(Math.max(4, "nine")); // NaNconsole.log(Math.max()); // -Infinity
console.log(Math.min()); // InfinityThe 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
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.