Difference between call and apply

What is the difference between call and apply in JavaScript?
Call and Apply serve similar purposes in JavaScript. Both functions allow you to control the meaning of 'this' inside the called function, but the difference is in how arguments are passed to the function. Apply expects the arguments in an array whereas Call expects them as a comma separated list - similar to standard function invocation. The first argument, in both cases, is the object reference that will represent 'this' inside the called function. In the example below we are calling the same printName function, but two different formatters are "injected" to provide a more flexible implementation.
var formatter1 = { createFullName: function(firstName,lastName){ return firstName + ' ' + lastName; } }; var formatter2 = { createFullName: function(firstName,lastName){ return lastName + ', ' + firstName; } }; function printName(firstName,lastName){ console.log(this.createFullName(firstName,lastName)); } //comma separated list or arguments printName.call(formatter1,'Joe','Smith'); //arguments are passed as an array printName.apply(formatter2,['Joe','Smith']);
javascript
Check out more questions and answers here