How to find maximum value in an array using spread operator in JavaScript?



In this article, we are going to discuss how to find the maximum value in an array using the spread operator in JavaScript.

For this you need to understand what is an array, what is spread operator and Math.max() method.

Array

An Array is a variable which is used to store different types of elements. Used to store a bunch of elements and access them with a single variable.

Declaration of array can be done in below methods.

var House = [ ]; // method 1 
var House = new Array(); // method 2

Spread operator (…)

This operator helps to copy the entirely or a part of the array or object into another array or object.

Syntax: console.log(...object)

Math.max() method

Math is a in-built object with methods and properties for mathematical constants. It’s not a function object. With the Number type, math is possible. With BigInt, it is unworkable.

To find maximum value in the array we have a many in-built functions, but using spread operator it became much more easier to find the maximum value in the array. Using math.max() method is quite difficult to pass the elements into it separately. So to avoid this complexity we use spread operator to find the maximum element.

Example 1

Finding maximum array using Math method

In the below scenario we have used math.max() instead of spread operator. This makes every element in the array to pass into the math function separately. If the number of elements are less set of elements it’s fine, If the elements are larger set of elements. It will be huge task to push each and every element into function individually.

<html> <body> <script> //Implementing the array var array = [10, 20, 30, 90, 120]; //Pushing every value of the array into math function var Max_array = Math.max(array[0], array[1], array[2], array[3], array[4]); //Printing all values of the array document.write("Values in the array:"); document.write(array); document.write("<br>") //Printing the maximum array document.write("The maximum value in the array is:") document.write(Max_array); </script> </body> </html>

Example 2

Finding maximum array using Spread operator

In this scenario, we have included the spread operator in the script to find the maximum element in the array. Here we doesn’t need to pass all the elements of the array into any function. We just have to make use of spread operator to avoid such heavy process. And this is a new way to find maximum element of array

<html> <body> <script> //Implementing the array and its values. var array = [6, 4, 8, 9, 12]; //Pushing values of the array using spread operator. var Max_array = Math.max(...array); //Printing elements of the array document.write("Values of the array are:"); document.write(array); document.write("<br>") // Maximum array value. document.write("The maximum value in the array is :"); document.write(Max_array); </script> </body> </html>

Example 3

User input values

In this script we have used spread operator and instead of defining the array and values in the script, we’ve taken array values from user.

<html> <body> <script> var array = []; // Empty array. var size = 5; // Size of the array. //Prompts to user to enter the array elements. for (var a = 0; a < size; a++) { array[a] = prompt('Enter the array Element ' + (a + 1)); } //Pushing values of the array using spread operator. var Max_array = Math.max(...array); //Printing Array values. document.write("Array and its values are: "); document.write(array); document.write("<br>") //Finding maximum value in array. document.write("The maximum value in the array is: "); document.write(Max_array); </script> </body> </html>

Advertisements