
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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 Articles
- How to find maximum value in an array using spread operator in JavaScript?
- How to find the minimum value of an array in JavaScript?
- Find the closest value of an array in JavaScript
- Function that returns the minimum and maximum value of an array in JavaScript
- How to find the maximum value in an R data frame?
- How to find the length of an array in JavaScript?
- How to find inside an array of objects the object that holds the highest value in JavaScript?
- How to find the maximum element of an Array using STL in C++?
- Find the maximum possible value of the minimum value of modified array in C++
- Find indexes of multiple minimum value in an array in JavaScript
- How to find the maximum value of all matrices stored in an R list?
- Write a program in Go language to find the element with the maximum value in an array
- Maximum length of mountain in an array using JavaScript
- How to modify an array value of a JSON object in JavaScript?
- JavaScript Finding the third maximum number in an array

Advertisements