5.7.2.2 min () and max () methods, 5.7.2.2min
Math objects also contain many methods to help complete simple and complex mathematical calculations.
The min () and max () methods are used to determine the minimum and maximum values of a group of values. Both methods can accept any number of numeric parameters, as shown in the following example:
Var max = Math. max (3, 54, 32, 16); alert (max); // 54var min = Math. min (3, 54, 32, 16); alert (min); // 3
For 3, 54, 32, and 16, Math. max () returns 54, while Math. min () returns 3. These two methods are often used to avoid unnecessary loops and determine the maximum number of a group in the if statement.
To find the maximum or minimum values in the array, use the apply () method as follows.
Var values = [1, 2, 3, 4, 5, 6, 7, 8]; var max = Math. max. apply (Math, values );
The key to this technique is to take the Math object as the first parameter of apply () and set this value correctly. Then, any array can be used as the second parameter.