In an interview, you often get the question “How do I get the maximum value of an array?” Such an interview question, this is a very basic interview, but there are many ways, today the more common five ways to record, if there are other methods, welcome to leave a message

The first:

Es6 extension operators...Math.max(... arr)Copy the code

The second:

Apply to realizeMath.max.apply(null,arr)
Copy the code

The third:

Cycle to achievelet max = arr[0];
    for (let i = 0; i < arr.length - 1; i++) {
        max = max < arr[i+1]? arr[i+1] : max
    }
Copy the code

Fourth:

Array sort() arr.sort((num1, num2) = > {
    return num1 - num2 < 0
    })
arr[0]
Copy the code

Fifth:

Iterative method reduce arr. Reduce ((num1, num2) = > {
        return num1 > num2 ? num1 : num2}
    )
Copy the code