'Why is the spread operator needed for Math.max()?

function findLongestWordLength(str) {
let arr = str.split(' '); 
let lengths = arr.map(word => word.length);

console.log(Math.max(lengths));
}

findLongestWordLength("The quick brown fox jumped over the lazy dog");

console.log(Math.max(lengths)) results in NaN, console.log(Math.Max(...lengths)) works. Why does lengths need to be spreaded? Math.Max takes an array as its argument, & lengths is an array? Thanks



Solution 1:[1]

Math.Max takes an array as its argument

This is not the case according to MDN:

The Math.max() function returns the largest of the zero or more numbers given as input parameters, or NaN if any parameter isn't a number and can't be converted into one.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 coreyward