Simply because Math.max() attempts to convert its arguments to numbers. An array does not convert into a number, so you get NaN.

Calling Math.max(...lengths) splits the lengths array into several arguments, which works (so long as they're all numbers, of course).

Answer from AKX on Stack Overflow
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ Math โ€บ max
Math.max() - JavaScript | MDN
July 20, 2025 - The Math.max() static method returns the largest of the numbers given as input parameters, or -Infinity if there are no parameters.
Discussions

How to use math.max with array
I've found Mozilla Developer Network (MDN) to be a great resource for JavaScript documentation. In this particular case, the last of the three examples they give right at the top of the page for Math.max() is exactly what you're looking for. As a PSA, when looking for JS tips, skip w3schools entirely. You're welcome. More on reddit.com
๐ŸŒ r/Bitburner
11
7
October 21, 2021
Understanding Math.max()
Math.max doesn't accept an array, just multiple arguments. More on stackoverflow.com
๐ŸŒ stackoverflow.com
java - How to use Math.min and Math.max in integer array - Stack Overflow
I want to find the maximum and minimum values in an integer array but I'm unable to use them . Eclipse throws this error The method min(int, int) in the type Math is not applicable for the More on stackoverflow.com
๐ŸŒ stackoverflow.com
JavaScript Math.max() Explained with Examples
Math Max Math.max() is a function that returns the largest value from a list of numeric values passed as parameters. If a non-numeric value is passed as a parameter, Math.max() will return NaN . An array of numeric values can be passed as a single parameter to Math.max() using either spread ... More on forum.freecodecamp.org
๐ŸŒ forum.freecodecamp.org
8
September 8, 2019
๐ŸŒ
W3Schools
w3schools.com โ€บ jsref โ€บ jsref_max.asp
W3Schools.com
The Math.max() method returns the number with the highest value.
๐ŸŒ
Programiz
programiz.com โ€บ javascript โ€บ library โ€บ math โ€บ max
JavaScript Math max()
The max() method finds the maximum value among the specified values and returns it. let numbers = Math.max(12, 4, 5, 9, 0, -3); console.log(numbers); // Output: 12
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-math-max-method
Java Math max() Method - GeeksforGeeks
May 14, 2025 - The max() method in Java is a part of java.lang.Math class. This is an inbuilt function in Java that returns maximum of two numbers.
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ 8 โ€บ docs โ€บ api โ€บ java โ€บ lang โ€บ Math.html
Math (Java Platform SE 8 )
October 20, 2025 - If the argument is positive infinity or any value greater than or equal to the value of Integer.MAX_VALUE, the result is equal to the value of Integer.MAX_VALUE.
Find elsewhere
๐ŸŒ
Substack
annamills.substack.com โ€บ p โ€บ automating-linkedin-search-by-getting
Automating LinkedIn search by getting Claude to build and test a new skill
2 weeks ago - async function scrollAndSearch(keyword, maxScrolls = 30) { for (let i = 0; i < maxScrolls; i++) { window.scrollTo(0, document.body.scrollHeight); await new Promise(r => setTimeout(r, 1500)); if (document.body.innerText.toLowerCase().includes(keyword.toLowerCase())) { const text = document.body.innerText; const idx = text.toLowerCase().indexOf(keyword.toLowerCase()); const start = Math.max(0, idx - 300); const end = Math.min(text.length, idx + 300); return { found: true, scrolls: i + 1, context: text.substring(start, end) }; } } return { found: false, scrolls: maxScrolls }; } scrollAndSearch('your keyword here')
๐ŸŒ
TradingView
tradingview.com โ€บ script โ€บ j3mz6K8i-zhumabayevv-sessions
Zhumabayevv_Sessions โ€” Indicator by zhumabayev147
2 weeks ago - ou are looking back from Monday ... math.min(asiaLow[1], low) else asiaLow[1] float asiaHigh = na asiaHigh := if asiaSession if asiaNewbar high else math.max(asiaHigh[1], high) else asiaHigh[1] int asiaStart = na asiaStart := if asiaSession if asiaNewbar time else ...
๐ŸŒ
Wikipedia
en.wikipedia.org โ€บ wiki โ€บ Maximum_and_minimum
Maximum and minimum - Wikipedia
4 days ago - In mathematical analysis, the maximum and minimum of a function are, respectively, the greatest and least value taken by the function. Known generically as extrema, they may be defined either within a given range (the local or relative extrema) ...
๐ŸŒ
freeCodeCamp
forum.freecodecamp.org โ€บ guide
JavaScript Math.max() Explained with Examples
September 8, 2019 - Math Max Math.max() is a function that returns the largest value from a list of numeric values passed as parameters. If a non-numeric value is passed as a parameter, Math.max() will return NaN .
๐ŸŒ
Medium
medium.com โ€บ @ehzevin โ€บ math-to-the-max-506d161122e7
Math to the Max!. Math.max() | by Elizabeth Zevin | Medium
October 17, 2019 - In the above function given the array1 we ask what is the max of that array. So that can check each item in the array, then we use the spread operator (โ€ฆ) to call the individual elements of the array. Simple! Another use is; letโ€™s say you have a bunch of inputs you are collecting from different sources, but you only want the largest one to show, you can place the inputs in the parenthesis and let Math.max() do all the work for you.
๐ŸŒ
SamanthaMing
samanthaming.com โ€บ basics โ€บ math-max
Math.max() | SamanthaMing.com
const array = [1, 20, 5]; const max = Math.max(...array); max; // 20
๐ŸŒ
Hugo
gohugo.io โ€บ functions โ€บ math โ€บ max
math.Max
April 10, 2025 - Returns the greater of all numbers. Accepts scalars, slices, or both.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ javascript-math-max-method
JavaScript Math max() Method - GeeksforGeeks
September 30, 2024 - The Math.max() method in JavaScript returns the largest value from a set of zero or more numbers.
๐ŸŒ
Maxmath
maxmath.fun
MaxMath โ€“ Maximise your mathematics
MaxMath is a comprehensive and practical mathematical program designed to tackle low numeracy skills among school students.
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ dotnet โ€บ api โ€บ java.lang.math.max
Math.Max Method (Java.Lang) | Microsoft Learn
[<Android.Runtime.Register("max", "(JJ)J", "")>] static member Max : int64 * int64 -> int64 ... Java documentation for java.lang.Math.max(long, long).
๐ŸŒ
Macmillan Education
macmillaneducation.es โ€บ home โ€บ international curriculum โ€บ maths โ€บ max-maths
Max-Maths - Macmillan - Macmillan
July 9, 2020 - MAX MATHS ยท Home / International Curriculum / Maths / Max-Maths ยท MAX MATHS ยท GO TO E-SHOP ยท It has been designed for International and English-medium schools and includes the following features to ensure a firm understanding and continued progression: Course for over 6 levels ยท