apply() runs a function right now, just like call(). The only difference is how you give it the arguments: instead of listing them one by one, you put them all in one array.
It returns whatever the function returns. It is handy when your values are already in an array, for example to find the biggest number in a list with Math.max.
Remember
func.apply(thisArg, argsArray);
thisArg → what "this" is inside func
argsArray → an array (or array-like) of arguments
null or undefined = no arguments
returns → whatever func returns
apply() runs the function immediately.
Syntax
Call apply() on any function. The first argument is the value for this; the second is an array that holds the arguments.
Optional. The value that this has inside the function. In strict mode it is used exactly as you pass it. In normal (non-strict) code, null and undefined are replaced by the global object. An arrow function ignores it.
argsArray
Optional. An array, or an array-like object (an object with a length and numbered keys), that holds the arguments. null and undefined mean "no arguments". A number, a string or true throws a TypeError.
Return Value
Return Value
Description
The function's result
Whatever the function returns is returned by apply().
TypeError (thrown)
argsArray is not an array, not an array-like object, and not null or undefined.
RangeError (thrown)
The array is extremely long, so the engine cannot pass that many arguments.
JavaScript Examples
Example 1 — Use an Array as the Argument List
Math.max() wants separate numbers, but the scores are in an array. apply() unpacks the array for it.
No second argument: 0
null: 0
Array of 3: 3
Array-like: 2
A number: TypeError
A string: TypeError
How It Works
Nothing to pass. No second argument, null and undefined all mean "no arguments", so the function counts 0.
Arrays and array-likes. An array of three items gives 3. An object with only { length: 2 } also works and gives 2, because apply() reads the length and treats the missing items as undefined.
Other types fail. A number or a string is not an array-like object, so apply() throws a TypeError. The catch blocks print the error name.
Compatibility
Browser & Runtime Support
apply() is one of the oldest JavaScript features. Every browser and every Node.js version supports it, and it accepts an array-like object in all current engines.
✓ ES3 · Works everywhere
func.apply()
Supported in all browsers, including old ones, and in Node.js, Deno and Bun.
ES3Standard since
Google ChromeAll versions · Desktop & Mobile
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
OperaAll versions
Full support
Samsung InternetAll versions · Android
Full support
Android WebViewAll versions
Full support
Node.jsAll versions · Server runtime
Supported
DenoAll versions · JavaScript runtime
Supported
BunAll versions · JavaScript runtime
Supported
Internet ExplorerAll versions
Full support
func.apply()Excellent
Bottom line: You can use apply() anywhere. Very old engines (before ES5) only accepted real arrays and the arguments object as the second argument.
Frequently Asked Questions
It runs a function immediately, with the value you choose as this and with the arguments taken from an array. It returns whatever the function returns. You write it as myFunction.apply(thisValue, [arg1, arg2]).
Only how the arguments are passed. apply() takes one array (fn.apply(obj, [1, 2, 3])); call() takes them one by one (fn.call(obj, 1, 2, 3)). A simple way to remember it: "A" in apply stands for array.
Often not. Math.max(...numbers) does what Math.max.apply(null, numbers) does. apply() is still useful when you also need to set this, and you will see it in a lot of older code, so it is worth knowing.
The function simply runs with no arguments. null and undefined are both allowed as the second argument. A number, a string or a boolean is not allowed and throws a TypeError.
Every item becomes one argument, and engines limit how many arguments one call can have. For arrays with hundreds of thousands of items, loop over the array or use reduce() instead of passing everything to apply().