JS Built-ins
Functionapplyintermediate

Function.prototype.apply() β€” rest params sum

πŸ“– Usage Example
function greet(hi) { return `${hi}, ${this.name}` }
greet.apply({ name: 'Ana' }, ['Hello'])  // β†’ 'Hello, Ana'

apply() combines this binding and argument spreading in one call.

apply() with rest parameters

Functions using rest parameters gather all arguments. apply() lets you pass an array that becomes the rest parameter.

Challenge: Use sum.apply(null, [1, 2, 3]) and verify the result is 6.

πŸ’‘ Hints (1)
  • β€’ `apply(thisArg, array)` β€” array elements become the function's arguments.