Home > Web Front-end > JS Tutorial > 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

青灯夜游
Release: 2021-06-22 16:59:45
Original
2808 people have browsed it

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
Copy after login

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
Copy after login

[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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template