return max value of array javascript code example
Example 1: max value in array javascript
const arr = [1, 5, 3, 5, 2];
const max = arr.reduce((a, b) => { return Math.max(a, b) });
const max = Math.max.apply(null, arr);
const max = Math.max(...arr);
Example 2: js max value of array
let numbers = [4, 13, 27, 0, -5];
Math.max.apply(null, numbers);
Example 3: get max element of array javascript
const maxMinArray = (a,b,...c) =>{
let array = [a,b];
let newArray = array.concat(c);
let max = 0;
for(let number of newArray){
if(max<number){
max = number;
}
}
let min = max;
for(let number of newArray){
if(number<min){
min = number;
}
}
return console.log(max + ' ' + min)
}
maxMinArray(12,14,15,23)
Example 4: get largest number in array javascript
const array1 = [1, 3, 2];
Math.max(...array1);