How to Loop Through an Array and Find the Biggest Number in JavaScript

1 year ago admin Javascript

In today's lesson, we will see how to loop through an array of numbers and find the biggest number in javascript using ES6 magic.


Get the biggest number in javascript using ES6

To achieve that all you need to do is to use the spread operator and check for the biggest number using the Math.max static method.

                                                        
                                                                                                                        
const numbers = [2, 5, 10, 30, 8];
console.log(Math.max(...numbers));
//returns 30

Using the reduce method

You can achieve the same result using the reduce static method.

                                                            
                                                                                                                                
const numbers = [2, 5, 10, 30, 8];
console.log(numbers.reduce((number,max) => number > max ? number : max, 0));
//returns 30