What is the difference between call, apply, and bind in JavaScript
· Category: JavaScript
Short answer
call and apply invoke a function immediately with a specified this context, while bind returns a new function with this permanently set for later invocation.
Key differences
| Method | Invocation | Arguments | Use case |
|---|---|---|---|
call |
Immediate | Comma-separated | Pass known discrete args |
apply |
Immediate | Array-like | Pass dynamic array of args |
bind |
Deferred | Partial application | Create callbacks with fixed context |
Example
function introduce(greeting, punctuation) {
console.log(`${greeting}, I'm ${this.name}${punctuation}`);
}
const person = { name: 'Sara' };
introduce.call(person, 'Hello', '!');
introduce.apply(person, ['Hi', '.']);
const sayHello = introduce.bind(person, 'Hey');
sayHello('?');
When to use each
- Use
callwhen you have a fixed list of arguments. - Use
applywhen arguments already live in an array. - Use
bindwhen you need to pass a method as a callback without losingthis.