Home  >  Article  >  Web Front-end  >  How to find the maximum and minimum values ​​of an array in javascript

How to find the maximum and minimum values ​​of an array in javascript

青灯夜游
青灯夜游Original
2021-06-22 16:56:202748browse

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.

How to find the maximum and minimum values ​​of an array in javascript

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn