Up to this point, I thought “calling” and “invoking” a function meant the same thing. However, in a YouTube tutorial it said to invoke a function by calling it. My first thought was that the wording was a mistake, but on W3Schools’ page on Function Invocation, it says:
It is common to use the term “call a function” instead of “invoke a function” … In this tutorial, we will use invoke, because a JavaScript function can be invoked without being called.
Okay, so there’s a difference. What is it?
Advertisement
Answer
Your reference text:
It is common to use the term “call a function” instead of “invoke a function” … In this tutorial, we will use invoke, because a JavaScript function can be invoked without being called.
Now let me rephrase it:
It is common to use the term “call a function” instead of “invoke a function” … In this tutorial, we will use the term invoke instead of call, because a JavaScript function can be invoked indirectly like
fn.call()
andfn.apply()
without being called directly likefn()
.
So, when I do fn()
, it’s invoked directly and when I do it like fn.call()
, it’s invoked indirectly but in both cases, the function is being invoked. Otherwise, I see no difference here and I can also say that I can call a function in many ways, for example:
fn(); // I'm calling it fn.call(); // I'm calling it fn.apply(); // I'm calling it
So, the difference is semantic but both are interchangeable, IMO. BTW, I wrote a comment above, under the question and I would like to put it here, which is:
IMO, that’s a misleading statement. Maybe there are some indication of call/apply or something else but it’s totally confusing.