Home >Web Front-end >JS Tutorial >How to find the minimum value of an array using JavaScript

How to find the minimum value of an array using JavaScript

青灯夜游
青灯夜游Original
2021-10-14 16:52:1711352browse

In JS, you can use the min() method of the Math object and the extension operator "..." to find the minimum value of the array; the min() method can return the minimum value in the specified number list. Numeric, the spread operator can convert an array into a comma-separated sequence of parameters, implementing the syntax "Math.min(...arr)".

How to find the minimum value of an array using JavaScript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

In JavaScript, you can use the min() method of the Math object and the spread operator "..." to find the minimum value of the array.

Implementation code:

var arr=[2,6,1,5,22,3,66,12,9];
var min=Math.min(...arr);
console.log("最小值为:"+min);

Output result:

How to find the minimum value of an array using JavaScript

##Description:

min( ) method returns the number with the smallest value in the specified list of numbers. Grammar format:

Math.min(n1,n2,n3,...,nX)

The spread operator (...) was introduced in es6, which is used to convert an array into a parameter sequence separated by commas. It is commonly used in function calls when the number of parameters is uncertain. Arrays The usage of merge

is as follows:

1. Get the maximum value of the array

function getMaxArray(arr){
    return Math.max(...arr);
}

2. Merge the array

var arr=[1,2,3,4];
var arr1=[1,2];
var arr2=[...arr,...arr1]//arr+arr1合并数组

[Recommended learning:

javascript advanced tutorial

The above is the detailed content of How to find the minimum value of an array using 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