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

Popular Tutorials

Related Tutorials

How to Get the Index of a Value in an Array in JavaScript

In this lesson, we will see how to get the index of a value in an array in JavaScript, sometimes we...


How to Join Elements of an Array in JavaScript

In this lesson, we will see how to join elements of an array in JavaScript, sometimes we want to joi...


How to Check if an Array Includes a Value in JavaScript

In this lesson, we will see how to check if an array includes a value in JavaScript, sometimes we wa...


How to Remove a Specific Value from an Array in JavaScript

In this lesson, we will see how to remove a specific value from an array in JavaScript, sometimes we...


How to Add Value to an Existing Array in JavaScript

In this lesson, we will see how to add value to an existing array in JavaScript, sometimes we want t...


How to Remove Whitespace from a String in JavaScript

In this lesson, we will see how to remove whitespace from a string in JavaScript, sometimes we want&...


How to Remove Spaces from a String in JavaScript

In this lesson, we will see how to remove spaces from a string in JavaScript, sometimes we want to r...


How to Check if a String Starts with a Character in JavaScript

In this lesson, we will see how to check if a string starts with a desired character in JavaScript,...


How to Check if a String Contains a Substring in JavaScript

In this lesson, we will see how to check if a string contains a substring in JavaScript, sometimes w...


How Do you Check if a String ends with a Character in JavaScript

In this lesson, we will see how to check if a string ends with a desired character in JavaScript, so...