Reflect.apply(target, thisArgument, argumentsList)calls a function for you. You give it the function, the value to use as this, and an array with the arguments. It returns whatever the function returns.
It does the same job as fn.apply(thisArgument, argumentsList), but as a normal function call. That means it still works when the function has its own property named apply, or when the function has no apply at all.
Remember
Reflect.apply(target, thisArgument, argumentsList)
target → the function to call
thisArgument → the value of this inside it
argumentsList → an array (or array-like) of arguments
returns the result of the function
throws TypeError if target is not a function
Syntax
Call it on the Reflect object. Pass the function, a value for this, and an array of arguments.
JavaScript
function add(a, b) {
return a + b;
}
console.log(Reflect.apply(add, undefined, [2, 3])); // 5
console.log(Reflect.apply(Math.max, undefined, [4, 9, 1])); // 9
Call
What it does
Reflect.apply(fn, undefined, [1, 2])
Call fn with the arguments 1 and 2, and no special this.
Reflect.apply(fn, obj, [])
Call fn so that this is obj.
fn.apply(obj, [1, 2])
The older way. It breaks if fn has its own apply property.
fn.call(obj, 1, 2)
Like apply(), but the arguments are listed one by one.
// ECMAScript 2015 (ES6)
Reflect.apply(target, thisArgument, argumentsList)
// target : a function
// thisArgument : the value of this
// argumentsList : an array-like object
// returns : what target returns
// throws : TypeError if target is not callable
Parameters
Parameter
Description
target
Required. The function to call. If it is not a function, a TypeError is thrown.
thisArgument
Required. The value to use as this inside the function. Pass undefined when the function does not use this.
argumentsList
Required. An array, or an array-like object, with the arguments. Pass an empty array [] for no arguments. A number or undefined here throws a TypeError.
Return Value
Return Value
Description
Any value
The value that the called function returns. If the function throws an error, that error is passed on to you.
TypeError (thrown)
target is not a function, or argumentsList is not an object.
JavaScript Examples
Example 1 — Call a Function with an Array of Arguments
Find the biggest number, take a letter from a string, and make text upper case, all by passing an array of arguments.
Reflect.apply: 5
add.apply: TypeError
Not a function: TypeError
Not an array: TypeError
How It Works
Replace apply. The line add.apply = "oops" hides the real apply method. add.apply(...) now tries to call a string and throws a TypeError.
Reflect still works.Reflect.apply() does not look for a property on the function, so it calls add normally and returns 5.
Wrong inputs. A string is not callable, and 5 is not an array of arguments, so both calls throw a TypeError.
Compatibility
Browser & Runtime Support
Reflect.apply() is part of ES2015 (ES6), together with the Reflect object. Every current browser and Node.js has it.
✓ ES2015 · Widely supported
Reflect.apply()
Supported in Chrome 49+, Edge 12+, Firefox 42+, Safari 10+, Opera 36+ and Node.js 6+. Internet Explorer never got it.
ES2015Standard since
Google Chrome49+ · Desktop & Mobile
Full support
Microsoft Edge12+ · Chromium and legacy
Full support
Mozilla Firefox42+ · Desktop & Mobile
Full support
Apple Safari10+ · macOS & iOS
Full support
Opera36+ · Chromium based
Full support
Samsung InternetRecent versions · Android
Supported
Android WebView49+ · Chromium based
Supported
Node.js6+ · Server runtime
Supported
DenoSupported · JavaScript runtime
Supported
BunSupported · JavaScript runtime
Supported
Internet ExplorerNo native support
Not supported
Reflect.apply()Excellent
Bottom line: Safe to use in every current browser and in Node.js. The only thing to plan for is Internet Explorer, which has no Reflect at all.
Frequently Asked Questions
It calls a function with a value for this and an array of arguments, and returns the result. It is the function-style version of fn.apply(thisArgument, argumentsList).
They do the same job. Reflect.apply() takes the function as its first argument, so it does not depend on the function having a working apply property. It also throws a clear TypeError for wrong input.
Often not. Math.max(...numbers) is shorter. Use Reflect.apply() when you must also set this, when you are forwarding a call inside a Proxy, or when the function might have its own apply property.
Reflect.apply() throws a TypeError. It also throws a TypeError when the third argument is not an array or an array-like object, for example a number.
A Proxy can intercept calls with an apply trap. Inside the trap, Reflect.apply(target, thisArg, args) runs the original function with the original behaviour, so you can add logging or checks around it.