find max value in an array in javascript

To find the maximum value in an array in JavaScript, you can use the Math.max() method along with the spread operator (...) to expand the array. Here's an example:

index.tsx
const array = [5, 10, 2, 8, 3];
const maxVal = Math.max(...array);
console.log(maxVal); // Output: 10
102 chars
4 lines

In the above code, the Math.max() method is applied to the spread version of the array. This returns the maximum value from the array, which is then assigned to the maxVal variable. Finally, the console.log() function is used to print the maxVal variable to the console.

gistlibby LogSnag