JS Built-ins
Functionapplybeginner

Function.prototype.apply() β€” Math.max with array

πŸ“– Usage Example
const nums = [5, 1, 9, 3]
Math.max.apply(null, nums)  // β†’ 9

Use apply() to spread an array as individual arguments to a function.

apply() with Math.max

Because apply() spreads an array as arguments, it is perfect for calling Math.max with an array of numbers.

Challenge: Use Math.max.apply(null, [1, 5, 3]) and verify the result is 5.

πŸ’‘ Hints (1)
  • β€’ Before spread syntax existed, `apply` was the idiomatic way to spread an array into `Math.max`.