Methods to change Context – CALL and APPLY: Essential JavaScript Concepts

less than 1 minute read

In our previous article JavaScript Concepts: What Is this Keyword | 0004-08 We read about ‘This’ concept of JavaScript which refers to calling context of the function. Now we will see, what are the available possible ways to call the context we have.

In our video tutorial JavaScript Concepts: what Is this Keyword | part 2 | 0004-09 We have explained two different ways of calling context, one is CALL and other one is APPLY. One very common thing that strikes our mind while writing JavaScript is when to use Call and when to use Apply. This question is very nicely explained in our video session with some practical examples.

Both methods Call() and Apply() serve the exact same purpose. The only difference between how they work is that Call() expects all parameters to be passed in individually, whereas Apply() expects an array of all of our parameters. Apply lets you invoke the function with arguments as an array; Call requires the parameters be listed explicitly. A useful mnemonic is "A for array and C for comma."

Pseudo syntax:

theFunction.apply(valueForThis, arrayOfArgs)

theFunction.call(valueForThis, arg1, arg2, ...)

Comments