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).
MDN Web Docs
developer.mozilla.org โบ en-US โบ docs โบ Web โบ JavaScript โบ Reference โบ Global_Objects โบ Math โบ max
Math.max() - JavaScript | MDN
The Math.max() static method returns the largest of the numbers given as input parameters, or -Infinity if there are no parameters.
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
Understanding Math.max()
Math.max doesn't accept an array, just multiple arguments. More on 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
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
Videos
05:23
How to Find Maximum and Minimum in an Array in JavaScript ...
01:00
JavaScript Methods | Math.min() and Math.max() - YouTube
JAVA || How to use Math.min and Math.max - YouTube
00:56
JavaScript Math max() method - JavaScript Tutorial for Beginners ...
00:32
Math max() method in JavaScript | JavaScript Tutorial For Beginners ...
03:24
Max Maths 2nd Edition: A Look Inside Teacher Guides - YouTube
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
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.
Reddit
reddit.com โบ r/bitburner โบ how to use math.max with array
r/Bitburner on Reddit: How to use math.max with array
October 21, 2021 -
I'm trying to get the maximum value in an array. What's the correct syntax to do this?
Alternative, is there a way to compare different variables and get the name of the variable with the highest value? Like
var memory = 10 var cores = 20 var level = 200 var node = 5 -> return "level"
Top answer 1 of 3
7
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.
2 of 3
3
You could use destructuring spread syntax: Math.max(...arr) That would pass all entries of the array as individual arguments to the function. Alternatively you could use "reduce", which would be a useful function to have in your toolbelt in general anyway. I don't remember the exact syntax rn, but I believe it looks something like: arr.reduce((a,b) => Math.max(a,b)) Someone will want to verify and/or correct me on this, and probably expand on it a bit.
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')
Top answer 1 of 4
5
These function expect just two arguments. If you want the minimum of an array you can use IntStream.
int[] a = { 1, 5, 6 };
int max = IntStream.of(a).max().orElse(Integer.MIN_VALUE);
int min = IntStream.of(a).min().orElse(Integer.MAX_VALUE);
2 of 4
3
You can simply used in-build java Collection and Arrays to sort out this problem. You just need to import them and use it.
Please check below code.
import java.util.Arrays;
import java.util.Collections;
public class getMinNMax {
public static void main(String[] args) {
Integer[] num = { 2, 11, 55, 99 };
int min = Collections.min(Arrays.asList(num));
int max = Collections.max(Arrays.asList(num));
System.out.println("Minimum number of array is : " + min);
System.out.println("Maximum number of array is : " + max);
}
}
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) ...
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.
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).