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

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response