DEV Community

Discussion on: How many way to find max number in array on Javascript.

Collapse
 
bwca profile image
Volodymyr Yepishev

I think what's funny is that with javascript's dynamic typing sometimes it is a good idea to ensure the array you are using is in fact an array of numbers, i.e.

let array = [-1, 10, 30, 45, 5, 6, 89, 17, "Bob"];
console.log(array.reduce((element,max) => element > max ? element : max, 0));
Enter fullscreen mode Exit fullscreen mode

would produce "Bob" as the max number, which is hilarious :D