How to find max value in array in javascript

Arrays are the fundamental data structure in javascript. We can store multiple forms of data inside an array such as numbers, strings, booleans, objects, arrays, functions, etc. Arrays use zero based-indexing.

You can use math.max() function to find the max value in an array in javascript. To find the max value you should also be familiar with the spread operator ().

The Spread operator usually passes the array elements as an argument in the function. You can see the code example below:

const numbers = [1, 5, 4, 10, 9];
const maxValue = Math.max(...numbers);
console.log(maxValue); // Output: 10

Code Explanation

In the above code, we have a constant variable “numbers” in which the array containing numbers [1, 5, 4, 10, 9] is placed.

In the next step, we have declared a constant variable “maxValue” in which we have used the Math.max(numbers) function this function will return the max value present in the array of numbers.

In the last step, the value of the constant variable “maxValue” is displayed in the console.

Similar Posts