JS Interview Question: What’s the difference between .call and .apply?

What’s the difference between .call and .apply?

RLyn Ben
2 min readJan 19, 2016

Recalling the basics
Remember, in JavaScript, everything are objects, even Functions, and every objects has their properties and methods. Both .apply and .call are methods of Function object.

How do .apply or .call work?
Invoking a function with .apply and .call allows us to point an object to the invoked function by passing in the object as first argument and second argument (and so on) as its values.
The function’s ‘this’ keyword will be manipulated when invoked with .apply or .call.
From what I understand, .apply and .call are methods we use to assign the ‘this’ keyword from the invoked function to reference to an object for the duration of the method invocation.

Below is a code example with commented explanation.

So what’s the difference between .apply and .call?
Besides passing in an argument to a .call or .apply methods that references to the ‘this’ keyword of an invoked function, we can also pass in a 2nd argument or more. A good mnemonic to explain their differences are:

.Call Counts the number of arguments separated by Comma
.call method accepts one or more arguments as objects and requires to be listed explicitly, means, it is a fixed number of arguments.

foo.call(object, “other argument”, “another one”);

while

.Apply uses Array as an Argument
.apply method requires an array as its 2nd argument. This method is used if you don’t know the number of arguments to be passed or the arguments is already in an array.

foo.apply(object, [“argument1”, “argument2”, “argument3”]);

** Open CodePen above for Code sample

Also

If you are looking for a platform to develop your clients’ website, check out my SAAS platform https://ecompurpl.com

EcomPurpl.com is the all-in-one solution for anyone looking to create a website. Templates, E-Commerce, Hosting, Galleries, Booking & Appointments, CRM, 24/7 support, and integration of your app are all included.

--

--

RLyn Ben

Software Engineer / Front-End Dev. Currently resides in Las Vegas. https://ecompurpl.com