

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to find maximum value in an array using spread operator in JavaScript?
We have logical methods and also many inbuilt methods to find a maximum value in an array, but the use of spread operator has made our task much easier to find the maximum value. Inbuilt method Math.max() is the most common method used to find a maximum value in an array. But in this method, we need to pass all the elements individually, making our task harder. So to alleviate this problem spread operator comes into the picture.
Example
In the following example, the spread operator doesn't accompany Math.max() function. Every value of an array is sent into the math function. It is fine if there is a small set of values, but in case of a large set of values, it is difficult to pass every element into math function.
<html> <body> <script> var array = [1,2,3]; var Max1 = Math.max(array); var Max2 = Math.max(array[1],array[1],array[2]) ; document.write(Max1); document.write("<br>"); document.write(Max2); </script> </body> </html>
Output
NaN 3
In the following example, spread operator (...) is used instead of sending each value into math function. This is a modern method used to find the maximum value in an array.
Example
<html> <body> <script> var array = [1,2,3]; var Max1 = Math.max(array); var Max2 = Math.max(...array) ; document.write(Max1); document.write("<br>"); document.write(Max2); </script> </body> </html>
Output
NaN 3
- Related Questions & Answers
- How to clone an array using spread operator in JavaScript?
- How to clone an object using spread operator in JavaScript?
- JavaScript Spread Operator
- How to find the maximum value of an array in JavaScript?
- What is spread Operator (...) in JavaScript?
- Spread operator for arrays in JavaScript
- Spread operator in function calls JavaScript
- Using JSON.stringify() to display spread operator result?
- Usage of rest parameter and spread operator in JavaScript?
- How to use spread operator to join two or more arrays in JavaScript?
- How to find the minimum value of an array in JavaScript?
- Maximum length of mountain in an array using JavaScript
- How to find the maximum element of an Array using STL in C++?
- Find the closest value of an array in JavaScript
- How to find the maximum value in an R data frame?