All three methods are used to set the value of this inside a function, but they differ in how and when the function is invoked.
1️⃣ call()
- Invokes the function immediately.
- Arguments are passed individually (comma-separated).
Example:
const person = { name: "Alice" };
function greet(greeting, punctuation) {
console.log(`${greeting}, ${this.name}${punctuation}`);
}
greet.call(person, "Hello", "!"); // Hello, Alice!
2️⃣ apply()
- Invokes the function immediately.
- Arguments are passed as an array.
Example:
greet.apply(person, ["Hi", "!!"]); // Hi, Alice!!
3️⃣ bind()
- Returns a new function with
thisbound to the specified object. - Does not call the function immediately.
- Arguments can be passed now or later when calling the bound function.
Example:
const greetAlice = greet.bind(person, "Hey");
greetAlice("?"); // Hey, Alice?
⚖️ Comparison Table
| Method | Calls Function Immediately? | Arguments Format | Returns New Function? |
|---|---|---|---|
call |
✅ Yes | Comma-separated | ❌ No |
apply |
✅ Yes | Array | ❌ No |
bind |
❌ No | Comma-separated (partial) | ✅ Yes |
🧩 In Short:
- call() → call now, args separately
- apply() → call now, args as array
- bind() → create a new function with bound
this, call later
This is useful for function borrowing, partial application, and controlling the this context in JavaScript.