JavaScript - Function apply()
JavaScript - Function apply() Method
Function apply() Method
The Functionapply()method in JavaScript allows us to invoke a function given a specific value forthisand arguments provided as an array.
The Function call() and apply() methods are very similar, but the main difference between them is function apply() method takes a single array containing all function arguments, and the function call() method takes individual arguments.
Same as the Function call() method, we can use the apply() method to manipulate thethisvalue and can assign an orbitrary object tothis.
Syntax
The syntax of the Function apply() method in JavaScriot is as follows β
func.apply(thisArg, [arg1, arg2, ... argN]);Parameters
- thisArgβ The βthisArgβ grument represents the function context. It is an object whose properties are needed to access the reference function using the βthisβ keyword.
- [arg1, arg2, β¦ argN]β They are arguments to pass to the function.
Return value
It returns the resultant value returned from the function.
Examples
Letβs understand JavaScript Function apply() method with the help of some examples.
Using apply() method without passing argument
In the below code, we have defined the test() function printing the message in the output.
We invoked the function in a standard way and used the apply() method without passing any argument to invoke the function. The output shows that the apply() method gives the same output as a normal function call.
Output
The function test is invoked!
The function test is invoked!Using apply() method with this argument only
In the below code, the βcarβ object contains three different properties. We passed the car object as a βthisArgβ argument of the apply() method.
In the showCar() function, we access the properties of the car object using the βthisβ keyword and print it into the output.
Output
The price of the OD Q6 is: 10000000Using apply() method with an array of function arguments
In the example below, we pass the nums object as the first argument of the apply() method. After that, we pass the array of arguments as an apply() methodβs argument.
In the printSum() function, we access the object properties using the βthisβ keyword and function arguments using the function parameters.
Output
Total sum is 87Using apply() method with built-in functions
You can also use the apply() method with built-in object methods. We can invoke the built-in object methods (such as Math.max or Math.min) using apply() method.
In the example below, we use apply() method with built-in JavaScript functions - Math.max and Math.min. We canβt directly use these methods for the arrays. We invoke the Math.max and Math.min methods using apply() method. We pass null as thisArg and the array of five integers as the function argument.
Output
Max element in the array: 9
Max element in the array:3Notice if you use Math.max() or Math.min() methods directly for arrays to find max or min element in the array, the result will be NaN.