When using the Function#apply
method, it's important to note that the first argument should be thisArg. If you pass the array as thisArg, it essentially calls Math#max
without any arguments.
According to the MDN docs:
If no arguments are given, the result is -Infinity.
To resolve this issue, make sure to set Math
or null
as the thisArg.
let max = Math.max.apply(Math, numeros);
let numeros = [1, 5, 10, 20, 100, 234];
let max = Math.max.apply(Math, numeros);
console.log(max);
As pointed out by @FelixKling, starting from ES6 you can utilize spread syntax for providing arguments.
Math.max(...numeros)
let numeros = [1, 5, 10, 20, 100, 234];
let max = Math.max(...numeros);
console.log(max);