In JavaScript, call, apply, and bind are methods used to explicitly set the this context of a function. This is crucial when you want to control what this refers to inside a function, especially when borrowing methods or working with callbacks.

💡 What is this?
this refers to the object that is executing the current function. By default, it can vary based on how a function is called.
call()
Calls a function with a specified this context and arguments passed individually.
📌 Deep Dive: Using call
function greet(greeting, punctuation) {
console.log(greeting + ', ' + this.name + punctuation);
}
const person = { name: 'Alice' };
greet.call(person, 'Hello', '!'); // Hello, Alice!
apply()
Similar to call, but arguments are passed as an array.
📌 Deep Dive: Using apply
greet.apply(person, ['Hi', '...']); // Hi, Alice...
bind()
Returns a new function permanently bound to a specified this context and optionally preset arguments. It does not call the function immediately.
📌 Deep Dive: Using bind
const greetAlice = greet.bind(person, 'Hey');
greetAlice('!!'); // Hey, Alice!!
| Method | Arguments | Returns | When to use |
|---|---|---|---|
call | List of arguments | Function result | Invoke immediately with this and arguments |
apply | Arguments as array | Function result | Invoke immediately with this and array arguments |
bind | List of arguments (optional) | New bound function | Create a function with fixed this and optional preset arguments |
💡 Key Point
call and apply invoke the function immediately, while bind returns a new function for later use.
⚠️ Common Mistake
Don't forget that bind does not call the function — you must call the returned function explicitly.
Quick Knowledge Check
Test what you just learned
Question 1 of 2
Which method immediately calls a function with a specified this and arguments passed as an array?
Question 2 of 2
What does bind return?
Loading results...