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

Related Tuorials

How to Check if a Tab is Active in JavaScript

In this lesson, we will see how to check if a tab is active in JavaScript, sometimes we want to know...


How to Add Multiple Data Types for Vue js Props

In this lesson, we will see how to add multiple data types for vue js props, sometimes when working...


How to Convert an Object to a Map in JavaScript

In this lesson, we will see how to convert an object to a map in JavaScript, sometimes we want to co...


How to Create an Array From a Map in JavaScript

In this lesson, we will see how to create an array from a map in JavaScript, sometimes we want to co...


How to Get a Key-Value Pair From a Map in JavaScript

In this lesson, we will see how to get a key-value pair from a map in JavaScript, sometimes we want...


How to Get the Keys of a Map in JavaScript

In this lesson, we will see how to get the keys of a map in JavaScript, sometimes we want to get all...


How to Get the Values of a Map in JavaScript

In this lesson, we will see how to get the values of a map in JavaScript, sometimes we want to get a...


How to Get the Size of a Map in JavaScript

In this lesson, we will see how to get the size of a map in JavaScript, sometimes we want to get the...


How to Remove a Value from a Map in JavaScript

In this lesson, we will see how to remove a value from a map in JavaScript, sometimes we want to rem...


How to Check if a Map Has a Value in JavaScript

In this lesson, we will see how to check if a map has a value in JavaScript, sometimes we want to ch...