# javascript implements a method summary that finds the maximum value in an array

• 2020-12-19 20:55:13
• OfStack

Method 1 (using recursive functions) :

``````
var arr = [9,8,55,66,49,68,109,55,33,6,2,1];

var max = arr[0];

function findMax( i ){
if( i == arr.length ) return max;
if( max < arr[i] ) max = arr[i];
findMax(i+1);
}

findMax(1);
console.log(max);

``````

Method 2 (loop traversal using for) :

``````
var arr = [9,8,55,66,49,68,109,55,33,6,2,1];
var max = arr[0];
for(var i = 1; i < arr.length; i++){
if( max < arr[i] ){
max = arr[i];
}
}

console.log(max);

``````

Method 3 (using apply to pass the array into the max method and return it directly) :

``````
Math.max.apply(null,[9,8,55,66,49,68,109,55,33,6,2,1])
``````

Note: In addition, there are many array sorting methods, after sorting, according to the new array index value to get the maximum/minimum value.

``````
var a=[1,2,3,5];

``````

Multidimensional arrays can be modified as follows:

``````
var a=[1,2,3,[5,6],[1,4,8]];
var ta=a.join(",").split(",");// into 1 Dimensional array