Array Find Max in JavaScript

Level 389 keystrokes

Math.max() combined with spread operator finds the maximum value in an array.

JavaScript code

// Define a function to find the maximum value
1function findMax(arr) {
// Math.max() takes separate numbers, so ...arr spreads the array into arguments
// Same as Math.max(3, 7, 2, 9, 1)
2 return Math.max(...arr);
3}
// Call the function and print the maximum value (9)
4console.log(findMax([3, 7, 2, 9, 1]));

Output

9
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this JavaScript code and how often they appear.

  • .× 5
  • ,× 4
  • (× 4
  • )× 4
  • ;× 2
  • [× 1
  • ]× 1
  • {× 1
  • }× 1

More JavaScript examples at level 3

← JavaScript typing practice