All three methods — call(), apply(), and bind() — are used to manually set the value of this inside a function.
They differ mainly in how arguments are passed and when the function is executed.
1️⃣ call()
- Invokes the function immediately.
- Passes arguments individually (comma-separated).
function greet(city, country) {
console.log(`Hello ${this.name} from ${city}, ${country}`);
}
const user = { name: "Teekam" };
greet.call(user, "Jaipur", "India");
// Output: Hello Teekam from Jaipur, India
✅ call() → call now, pass arguments one by one.
2️⃣ apply()
- Also invokes immediately, just like
call(). - But it expects arguments as an array.
greet.apply(user, ["Jaipur", "India"]);
// Output: Hello Teekam from Jaipur, India
✅ apply() → call now, pass arguments as an array.
3️⃣ bind()
- Does not invoke immediately.
- Returns a new function with
thispermanently bound. - You can call it later.
const greetUser = greet.bind(user, "Jaipur", "India");
greetUser();
// Output: Hello Teekam from Jaipur, India
✅ bind() → bind now, call later.
🔹 Summary Table
| Method | Executes Immediately? | How to Pass Arguments | Returns What? | Use Case |
|---|---|---|---|---|
| call() | ✅ Yes | Comma-separated | Function result | Simple function call |
| apply() | ✅ Yes | Array | Function result | Use when args are in an array |
| bind() | ❌ No | Comma-separated | New function | Use for callbacks or event handlers |
💡 In Short:
call()→ call function immediately with arguments listed.apply()→ call function immediately with arguments in an array.bind()→ return a new function with fixedthis, call later.