Getting the min or max value from an array of numbers in javascript

Nick Raphael - Mar 27 '20 - - Dev Community

Turns out this is simple. Well, it was simple anyway, but it's also quite concise.

I'll provide my code examples in javascript.

Let's suppose we have an array of number...

var myNumbers = [100, 50, 200];
Enter fullscreen mode Exit fullscreen mode

We can use swanky destructuring assignment, which I know more commonly as the spread operator.

var myNumbers = [100, 50, 200];
var myMinNumber = Math.min(...myNumbers);
var myMaxNumber = Math.max(...myNumbers);
Enter fullscreen mode Exit fullscreen mode

Short and sweet.

. . . . . . . . . . . . . . . . . . . . . . . . . .