call, apply, and bind are Function methods in JavaScript used to explicitly control the value of this inside a function.
๐น Why Do We Need call, apply, bind?
In JavaScript, this depends on how a function is called, not where it is defined.
These methods let us manually set this.
๐น call() — Invoke Immediately (Comma-separated arguments)
function greet(city, country) {
console.log(this.name, city, country);
}
const user = { name: "Teekam" };
greet.call(user, "Delhi", "India");
โ Output
Teekam Delhi India
๐ง When to use call
- Borrow methods from another object
- Pass arguments individually
- Immediate execution
๐น apply() — Invoke Immediately (Arguments as Array)
greet.apply(user, ["Delhi", "India"]);
๐ง Difference from call
- Arguments passed as array
- Useful when arguments are dynamic
๐น bind() — Returns a New Function (Does NOT Execute)
const boundGreet = greet.bind(user, "Delhi", "India");
boundGreet();
๐ง When to use bind
- Event handlers
- Call function later
- Preserve
thispermanently
๐ Key Differences (Quick Table)
| Method | Executes Immediately | Arguments | Returns |
|---|---|---|---|
| call | โ Yes | Comma-separated | Function result |
| apply | โ Yes | Array | Function result |
| bind | โ No | Comma / Array | New function |
๐งช Real-World Examples
โ Method Borrowing
const person1 = { name: "A" };
const person2 = { name: "B" };
function sayName() {
console.log(this.name);
}
sayName.call(person1); // A
sayName.call(person2); // B
โ apply with Math.max
Math.max.apply(null, [1, 5, 3]); // 5
โ bind in Event Handler
button.onclick = obj.handleClick.bind(obj);
๐ฏ Short Interview Answer
callandapplyinvoke a function immediately while settingthis.calltakes arguments individually,applytakes them as an array.bindreturns a new function withthispermanently bound, used when execution needs to be deferred.
โญ One-line summary
call and apply run now, bind runs later — all control this.