JS Shorts — Call and Apply

Prakhar Jaiswal
2 min readNov 22, 2020

The call and apply are both used to call a function using a desired this value and a set of parameters.

Both these functions accept the first argument as the value of this. The only difference between the two is that .call accepts the list of parameters as a comma separated list of function arguments while .apply accepts an array as the list of arguments. An easy way to remember this is Call -> Comma and Apply -> Array.

Notice the example above, it shows how the output can be changed by providing a desired value for the this keyword.

There are a wide range of situations where this comes in handy.

  • One of the most simple example is when we want to conditionally change the value of the scope in which the function executes.
  • Another situation is when we want to conditionally change the number of arguments provided to the function.
Image by Dhruvil Patel from Pixabay

— That’s all for today

--

--