Home > Article > Web Front-end > How to find the maximum and minimum values of an array in javascript
Methods to find the maximum and minimum values of an array: 1. Use the "Math.min.apply(null, arr)" and "Math.max.apply(null, arr)" statements. 2. Use the "Math.min(...arr)" and "Math.max(...arr)" statements.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
How to get the maximum and minimum values in a javascript array
1. Use the max/min method in Math
Can be achieved using apply. The input passed to apply is an array.
var arr = [22,13,6,55,30]; var max = Math.max.apply(null, arr); var min = Math.min.apply(null, arr); console.log(max, min) // 55,6
2. Use the ES6 spread operator
var arr = [22,13,6,55,30]; console.log(Math.min(...arr)); // 5 console.log(Math.max(...arr)); // 55
[Related recommendations: javascript learning tutorial]
The above is the detailed content of How to find the maximum and minimum values of an array in javascript. For more information, please follow other related articles on the PHP Chinese website!