Given an array of values [1,2,0,-5,8,3], how can I get the maximum value in JavaScript?
I know I can write a loop and keep track of the maximum value, but am looking for a more code-efficient way of doing so with the JavaScript syntax.
Given an array of values [1,2,0,-5,8,3], how can I get the maximum value in JavaScript?
I know I can write a loop and keep track of the maximum value, but am looking for a more code-efficient way of doing so with the JavaScript syntax.
 
    
     
    
    You can use Math.max and ES6 spread syntax (...):
let array = [1, 2, 0, -5, 8, 3];
console.log(Math.max(...array)); //=> 8 
    
     
    
    You can use the following:
yourArray.reduce((max, value) => {return Math.max(max, value)});
The reduce will itterate over values in the array, at each time returning the maximum of all of the values before the current one.
