Jennifer Bland header image
≡ Menu

The Difference Between .call and .apply in JavaScript

JavaScript has functions. When you execute that function then the contents of the function are performed. The easiest way to execute a function is to call it like getName().

JavaScript also provides two other methods to invoke that function:

  • getName.apply()
  • getName.call()

You might ask what is the difference between the two? Does one provide better performance than the other? Is one better to use than the other?

The best way to understand the difference between the two is to examine the pseudo syntax for each.

function.apply(thisArg, [argsArray]);
function.call(thisArg[, arg1[, arg2[, ...]]]);

.call

The call method calls a function with a given this value and arguments provided individually. The arguments are listed in comma separated format.

The first argument is the this value. This refers to the current object which is also know as the calling object.

With call you can write a method once and then inherit it in another object, without having to rewrite the method for the new object.

.apply

The apply method calls a function with a given this value and arguments provided as an array.

Just like call you can write a method once and the inherit it in another object, without having to rewrite the method for the new object.

The Difference

While the syntax of .apply() and .call() are almost identical, the fundamental difference is that call() accepts an argument list, while apply() accepts a single array of arguments.

You Might Want to Read

by Jennifer Bland

I am a Google Developers Expert. Entrepreneur. Mountain Climber. Neil Diamond fanatic. World traveler. MBA grad. Software Engineer. Interested in hiring me? Contact me at ratracegrad@gmail.com

0 comments… add one

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.