Get the maximum value in an array using JavaScript
Question:
How to get the maximum value in an array using JavaScript? Answer:
const myArray = [1, 14, 32, 7];
const maxValue = Math.max(...myArray);
console.log(maxValue); // 32
Description:
In modern JavaScript getting the maximum value in an array is quite simple by using the Math.max
function and the spread ...
operator.
The Math.max()
function returns the largest of the zero or more numbers given as input parameters, or NaN
if any parameter isn't a number and can't be converted into one. The result is -Infinity if no parameters are provided.
Reference:
Math.max reference
Share "How to get the maximum value in an array using JavaScript?"
Related snippets:
- Check if number is odd or even in JavaScript
- Get the absolute value of a number in JavaScript
- Round a number in JavaScript
- Generate random string in JavaScript
- Generate random date with JavaScript
- Generate a random number with fixed length using JavaScript
- Generate a random number in a range with JavaScript
- Get the minimum value in an array using JavaScript
- Get the maximum value in an array using JavaScript
- Convert a float number to integer in JavaScript
- Round a number to the nearest 10 using JavaScript
- Round down a number in JavaScript
- Round up a number in JavaScript
Tags:
max, value, array, javascript, get, find Technical term:
Get the maximum value in an array using JavaScript