

- 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 the maximum value of an array in JavaScript?
we can find the Maximum value of an array using Math.max() and sort() functions.
1) Math.max()
Math.max() function usually takes all the elements in an array and scrutinize each and every value to get the maximum value.It's methodology is discussed in the below example.
Example
<html> <body> <script> function maximum(value) { if (toString.call(value) !== "[object Array]") return false; return Math.max.apply(null, value); } document.write(maximum([6,39,55,1,44])); </script> </body> </html>
Output
55.
2) sort()
Using sort and compare functions we can arrange the elements in array in descending or ascending order, later on using the length property we can find the required value.
Example
<html> <body> <input type ="button" onclick="myFunction()" value = "clickme"> <p id="max"></p> <script> var numbers = [6,39,55,1,44]; document.getElementById("max").innerHTML = numbers; function myFunction() { numbers.sort(function(a, b){return a - b}); document.getElementById("max").innerHTML = numbers; document.write(numbers[numbers.length-1]); } </script> </body> </html>
From the above program the output is displayed as the following image
Output
Later on clicking the above click me button the maximum value is displayed as the following
55
- Related Questions & Answers
- How to find the minimum value of an array in JavaScript?
- How to find maximum value in an array using spread operator in JavaScript?
- Find the closest value of an array in JavaScript
- Function that returns the minimum and maximum value of an array in JavaScript
- Find the maximum possible value of the minimum value of modified array in C++
- How to find the maximum value in an R data frame?
- How to find the length of an array in JavaScript?
- How to find the maximum element of an Array using STL in C++?
- How to find the maximum value of all matrices stored in an R list?
- How to find inside an array of objects the object that holds the highest value in JavaScript?
- Find indexes of multiple minimum value in an array in JavaScript
- PHP program to find the maximum element in an array
- Write a program in Go language to find the element with the maximum value in an array
- Find the number of times a value of an object property occurs in an array with JavaScript?
- How to find the min/max element of an Array in JavaScript?
Advertisements