Back
JavaScriptMediumNew
What is the difference between call, apply, and bind?
All three control `this`; they differ in invocation timing and argument passing.
Ideal Answer
call, apply, and bind are methods on Function.prototype that let you set the this value of a function.
| Method | Invokes immediately? | Arguments |
|---|---|---|
call |
Yes | Comma-separated |
apply |
Yes | Array or array-like |
bind |
No — returns a new function | Optional preset args |
JavaScript
function greet(greeting, punctuation) {
return `${greeting}, ${this.name}${punctuation}`
}
const user = { name: 'Ada' }
greet.call(user, 'Hello', '!') // "Hello, Ada!"
greet.apply(user, ['Hello', '!']) // "Hello, Ada!"
const sayHi = greet.bind(user, 'Hi')
sayHi('?') // "Hi, Ada?"
When to use each
call/apply— borrow a method or invoke immediately with a specificthis.bind— create a permanently bound callback (e.g. event handlers, partial application).
Mnemonic: Call = Comma-separated args; Apply = Array of args.