JavaScript · The language round
What is the difference between call, apply and bind?
Short answer
call and apply invoke a function immediately with a chosen this; they differ in how arguments are passed. bind returns a new function with this and optional arguments fixed.
All three let you control this for a normal function. The difference is whether the function runs now and how arguments are supplied.
function introduce(greeting, punctuation) {
return greeting + ", " + this.name + punctuation;
}
const asha = { name: "Asha" };
introduce.call(asha, "Hi", "!"); // "Hi, Asha!"
introduce.apply(asha, ["Hello", "."]); // "Hello, Asha."
const bound = introduce.bind(asha, "Hey");
bound("?"); // "Hey, Asha?"The differences
call(thisArg, arg1, arg2)invokes immediately with individual argumentsapply(thisArg, [arg1, arg2])invokes immediately with an array-like argument listbind(thisArg, arg1)returns a new function and does not invoke it- Bound arguments are prepended to the arguments supplied later
bind is useful when passing a method as a callback, because the receiver is preserved even when the callback is called by another system.
class Counter {
count = 0;
increment() {
this.count += 1;
}
}
const counter = new Counter();
const increment = counter.increment.bind(counter);
setTimeout(() => {
increment();
console.log(counter.count); // 1
}, 0);this with call or apply. Arrow functions avoid the problem differently by capturing this lexically.Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1When would you use `apply` instead of `call`?
- 2Can you change `this` on a bound function?
- 3How is `bind` related to partial application?
- 4Why does passing a class method as a callback lose `this`?
Say it in the interview
call and apply both invoke a function immediately with a chosen this. call takes arguments one by one, while apply takes an array. bind does not invoke the function; it returns a new function with this fixed and can also pre-fill arguments. I use bind for callbacks that need a method's receiver, and I use call or apply when I want to borrow a function for one call.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live JavaScript mock interview and get scored feedback on depth, clarity and confidence.