Home >Web Front-end >JS Tutorial >What is the maximum value statement in javascript?
The maximum value statement in javascript is "Math.max(value1[,value2, ...])", where the "Math.max()" function is to return the maximum value in a set of numbers .
The operating environment of this article: windows7 system, javascript version 1.8.5, Dell G3 computer.
What is the maximum value statement in javascript?
Math.max() function returns the maximum value in a set of numbers.
Syntax
Math.max(value1[,value2, ...])
Parameters
value1, value2, ...
A set of numerical values
Return value
Returns the maximum value in a given set of numbers . If at least one of the given arguments cannot be converted to a number, NaN is returned.
Description
Since max is a static method of Math, it should be used like this: Math.max(), not a method of the created Math instance (Math is not a constructor).
If there are no parameters, the result is - Infinity.
If any parameter cannot be converted to a numeric value, the result is NaN.
Example
Using Math.max()
Math.max(10, 20); // 20 Math.max(-10, -20); // -10 Math.max(-10, 20); // 20
The following method uses the apply method to find the largest element in a numeric array. getMaxOfArray([1,2,3]) is equivalent to Math.max(1, 2, 3), but you can use getMaxOfArray () to operate on arrays of any length.
function getMaxOfArray(numArray) { return Math.max.apply(null, numArray); }
Or by using the latest spread operator, getting the maximum value in an array becomes easier.
var arr = [1, 2, 3]; var max = Math.max(...arr);
Recommended study: "javascript Advanced Tutorial"
The above is the detailed content of What is the maximum value statement in javascript?. For more information, please follow other related articles on the PHP Chinese website!