Call() Vs Apply() in Javascript
Hello Readers,
This article will help you to understand the difference between the call and apply function in javascript.
Below is the list of topic that is covered in this article:
· How to invoke a function using call.
· How to invoke a function using apply.
. Difference between call and apply.
So let’s see each and every topic one by one.
Introduction
Call and apply methods are used to invoke a function in javascript. The concept of this method comes into the picture when we want to explicitly bind an object with the function. In simple words, we can say, when we want to specify what this will be referring to in the function.
Invoking a function using the call method.
The method Call invokes the function and allows us to explicitly bind an object to a function and pass any number of arguments to a function.
Invoking a function using the apply method.
The apply method is similar to call the method and perform the same function. The difference between both lies in how the arguments are passed to the function.
Difference between call() and apply()
The only difference between both the function is how the arguments are pass while invoking the function.
While using call the arguments are passed as a comma-separated whereas in apply the arguments are passed as an array.
Conclusion
Call and Apply are interchangeable. You can decide whether it’s easier to send in an array or a comma-separated list of arguments
These concepts are radical to JavaScript and important to understand if you want to approach more advanced topics. I hope you got a better understanding of JavaScript Scope and things around it. If something just didn’t click, feel free to ask in the comments below.
That’s all folks,
Thanks for reading.