DEV Community

thebronxsystem
thebronxsystem

Posted on

Math.min array needs spread operator.

Hello all,

I can not confused why Math.min only works on array if you use the spread operator. I tried googling and reading mdn docs.

const array1 = [2, 3, 1];

console.log(Math.min(...array1));
// expected output: 1

if you do 
const array1 = [2, 3, 1];

console.log(Math.min(array1));
// expected output: NaN
Enter fullscreen mode Exit fullscreen mode

EDIT: ok i just read this
Spread syntax (...) allows an iterable such as an array expression or string to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected, or an object expression to be expanded in places where zero or more key-value pairs (for object literals) are expected.


console.log(sum(numbers));
// expected output: 6
Enter fullscreen mode Exit fullscreen mode

I didnt know this

Top comments (1)

Collapse
 
javierriveros profile image
Javier Riveros

Math.min is expecting two or more numbers passed as parameter instead of an array