JavaScript call(), apply() and bind(): Understand Function Borrowing and this
Quick Answer: JavaScript provides three methods to control a function's this value. Use call() to pass arguments separately, apply() to pass arguments as an array, and bind() to create a new function for later use.
All three help us reuse one function with different objects. The main difference is when the function runs and how arguments are passed.
1. Why Do We Need call(), apply() and bind()?
Imagine you manage a restaurant with 1,000 customer orders. Every order needs a receipt. Each receipt shows the customer, table number and total amount.
Would you write the same receipt function for every customer? Think about how difficult that becomes when the receipt format changes.
The problem: Repeating the same function
JavaScript objects can store their own data and functions. But repeating the same function inside every object creates unnecessary duplication.
Example 1: Repeating receipt methods for three restaurant orders
const orderOne = {
customer: "Priya",
table: 4,
total: 850,
printReceipt: function () {
console.log(`Customer: ${this.customer}`);
console.log(`Table: ${this.table}`);
console.log(`Total: Rs.${this.total}`);
}
};
const orderTwo = {
customer: "Ravi",
table: 7,
total: 1200,
printReceipt: function () {
console.log(`Customer: ${this.customer}`);
console.log(`Table: ${this.table}`);
console.log(`Total: Rs.${this.total}`);
}
};
const orderThree = {
customer: "Lakshmi",
table: 9,
total: 640,
printReceipt: function () {
console.log(`Customer: ${this.customer}`);
console.log(`Table: ${this.table}`);
console.log(`Total: Rs.${this.total}`);
}
};
orderOne.printReceipt();
orderTwo.printReceipt();
orderThree.printReceipt();
Expected output
Customer: Priya Table: 4 Total: Rs.850 Customer: Ravi Table: 7 Total: Rs.1200 Customer: Lakshmi Table: 9 Total: Rs.640
Important: This code works correctly. However, the same receipt logic appears three times.
What goes wrong when the application grows?
- We repeat the same function in many objects.
- A receipt change needs updates in many places.
- Testing repeated logic becomes harder.
- New order types may introduce more duplicated code.
Now think about 1,000 restaurant orders. Repeating the same function is difficult to maintain.
The solution: One function for every order
We can move the receipt logic into one shared function. Each order can use that function with its own data.
this = the object a normal function uses as its current context.
call(), apply() and bind() let us choose that context.
Example 2: One reusable receipt function
"use strict";
const orderOne = {
customer: "Priya",
table: 4,
total: 850
};
const orderTwo = {
customer: "Ravi",
table: 7,
total: 1200
};
const orderThree = {
customer: "Lakshmi",
table: 9,
total: 640
};
function printReceipt(currency) {
console.log(`Customer: ${this.customer}`);
console.log(`Table: ${this.table}`);
console.log(`Total: ${currency}${this.total}`);
}
printReceipt.call(orderOne, "Rs.");
printReceipt.apply(orderTwo, ["Rs."]);
const lakshmiReceipt = printReceipt.bind(orderThree, "Rs.");
lakshmiReceipt();
Expected output
Customer: Priya Table: 4 Total: Rs.850 Customer: Ravi Table: 7 Total: Rs.1200 Customer: Lakshmi Table: 9 Total: Rs.640
We now have one receipt function for all three orders. Each method supplies the required order context differently.
Without and with reusable functions
| Without shared function | With call, apply or bind |
|---|---|
| Each order stores its own receipt method. | One receipt function serves every order. |
| Receipt changes need repeated updates. | Receipt changes happen in one place. |
| Function logic is duplicated. | Function logic is reused. |
| Each method uses its own object context. | The calling code supplies the object context. |
The idea to remember
- this = the current object context used by a normal function.
- call() = run a function now with a chosen object.
- apply() = run a function now with arguments inside an array.
- bind() = create a new function with a chosen object context.
- Function borrowing = using one object's function with another object.
2. How Does call() Work in JavaScript?
call() runs a function immediately. Its first argument becomes the function's this value.
Any remaining arguments are passed to the function one by one.
Syntax
functionName.call(thisValue, argumentOne, argumentTwo, ...);
Here is how the restaurant receipt function uses call().
Example 3: Call the receipt function for Priya's order
"use strict";
const priyaOrder = {
customer: "Priya",
table: 4,
total: 850
};
function printReceipt(currency, message) {
console.log(message);
console.log(`Customer: ${this.customer}`);
console.log(`Table: ${this.table}`);
console.log(`Total: ${currency}${this.total}`);
}
printReceipt.call(
priyaOrder,
"Rs.",
"Restaurant receipt"
);
Expected output
Restaurant receipt Customer: Priya Table: 4 Total: Rs.850
Understand the call() execution flow
call(): Immediate function execution
Watch how the order object becomes the function context.
Animation: The function receives the order context, then prints its receipt immediately.
Read the call carefully:
priyaOrderbecomes the value ofthis."Rs."becomes the first function argument."Restaurant receipt"becomes the second function argument.- The function runs immediately when
call()executes.
3. How Does apply() Work in JavaScript?
apply() also runs a function immediately. It sets this in the same way as call().
The difference is how we pass the function arguments. With apply(), we pass them inside an array or array-like value.
Syntax
functionName.apply(thisValue, [argumentOne, argumentTwo]);
Example 4: Print Ravi's restaurant receipt using apply()
"use strict";
const raviOrder = {
customer: "Ravi",
table: 7,
total: 1200
};
function printReceipt(currency, message) {
console.log(message);
console.log(`Customer: ${this.customer}`);
console.log(`Table: ${this.table}`);
console.log(`Total: ${currency}${this.total}`);
}
printReceipt.apply(raviOrder, [
"Rs.",
"Restaurant receipt"
]);
Expected output
Restaurant receipt Customer: Ravi Table: 7 Total: Rs.1200
Understand the apply() execution flow
apply(): Array arguments are passed together
Watch the argument array reach the function before execution.
Animation: The order context and argument array are supplied before the receipt runs.
Notice the second argument in apply(). It contains both values in one array.
- The first argument sets
thistoraviOrder. - The second argument contains the function arguments.
- The function runs immediately after the values are supplied.
Remember: apply() accepts an array or array-like argument list. It does not require an array in every possible case.
4. How Does bind() Work in JavaScript?
bind() behaves differently from call() and apply().
It does not run the original function immediately. Instead, it returns a new function with a chosen this value.
We can call that new function later. We can also set some arguments in advance.
Syntax
const newFunction = functionName.bind(thisValue, argumentOne, argumentTwo);
Example 5: Prepare Lakshmi's receipt for later
"use strict";
const lakshmiOrder = {
customer: "Lakshmi",
table: 9,
total: 640
};
function printReceipt(currency, message) {
console.log(message);
console.log(`Customer: ${this.customer}`);
console.log(`Table: ${this.table}`);
console.log(`Total: ${currency}${this.total}`);
}
const lakshmiReceipt = printReceipt.bind(
lakshmiOrder,
"Rs."
);
console.log("Receipt function is ready.");
lakshmiReceipt("Restaurant receipt");
Expected output
Receipt function is ready. Restaurant receipt Customer: Lakshmi Table: 9 Total: Rs.640
The receipt does not print when bind() runs. It prints when we call lakshmiReceipt().
Understand the bind() execution flow
bind(): Prepare now, execute later
The binding step creates a function. A later call runs it.
Animation: Binding prepares a reusable function. The final step executes it later.
Here is what happens in the example:
bind()fixesthistolakshmiOrder."Rs."is saved as the first argument.lakshmiReceiptstores the new function.- The later function call supplies the remaining argument.
5. call() vs apply() vs bind(): Key Differences
| Feature | call() | apply() | bind() |
|---|---|---|---|
| Execution | Immediate | Immediate | Later, when returned function is called |
| Sets this | Yes | Yes | Yes, on the returned function |
| Arguments | Separate values | Array or array-like value | Separate values, saved for later |
| Returns | Function result | Function result | A new function |
| Best use | Immediate function call | Arguments already grouped together | Save a function for later use |
One example to remember all three
// call: arguments are passed separately
printReceipt.call(order, "Rs.", "Receipt");
// apply: arguments are passed in an array
printReceipt.apply(order, ["Rs.", "Receipt"]);
// bind: returns a function for later use
const receipt = printReceipt.bind(order, "Rs.");
receipt("Receipt");
6. Practical Uses in Real JavaScript Applications
Use call() to borrow a method
Sometimes, one object has a useful method. Another object needs the same behavior. We can borrow that method without copying it.
Example 6: Reuse a restaurant order summary method
"use strict";
const restaurantOrder = {
customer: "Priya",
table: 4,
showOrder: function () {
console.log(`${this.customer} has order for table ${this.table}`);
}
};
const takeawayOrder = {
customer: "Ravi",
table: "Takeaway"
};
restaurantOrder.showOrder.call(takeawayOrder);
Expected output
Ravi has order for table Takeaway
The method belongs to restaurantOrder. But call() runs it with takeawayOrder as its context.
Use apply() when arguments are already grouped
Suppose a restaurant receives several item prices in an array. We can use Math.max() with apply() to find the highest price.
Example 7: Find the highest menu item price
const menuPrices = [120, 250, 180, 320];
const highestPrice = Math.max.apply(null, menuPrices);
console.log(highestPrice);
Expected output
320
Here, Math.max() does not need an object context. We use null for the first argument.
Modern alternative: The spread operator is usually simpler for this case. Use Math.max(...menuPrices) in modern JavaScript.
Use bind() to preserve this for a callback
A callback is a function passed to another function for later execution.
Some APIs call your function later. In those cases, its this value may not be the object you expected.
Example 8: Keep the restaurant context in a callback
"use strict";
const restaurant = {
name: "Upgradix Restaurant",
showName: function () {
console.log(`Welcome to ${this.name}`);
}
};
const showRestaurantName = restaurant.showName.bind(restaurant);
function runWelcomeMessage(callback) {
callback();
}
runWelcomeMessage(showRestaurantName);
Expected output
Welcome to Upgradix Restaurant
bind() keeps the restaurant context. The callback can run without losing it.
7. Common Mistakes and Best Practices
Mistake 1: Expecting bind() to execute immediately
Wrong: This code creates a function but never calls it.
const receipt = printReceipt.bind(order);
Correct: Call the returned function when you need the receipt.
const receipt = printReceipt.bind(order);
receipt();
Rule: bind() prepares a function. It does not execute it.
Mistake 2: Passing separate arguments to apply()
Wrong: The second argument should be an array or array-like value.
// Incorrect argument format
printReceipt.apply(order, "Rs.", "Receipt");
Correct: Group the arguments inside an array.
printReceipt.apply(order, ["Rs.", "Receipt"]);
Rule: Use an argument array with apply().
Mistake 3: Expecting call() to change an arrow function's this
Arrow functions do not create their own this value. They use it from their surrounding scope.
Wrong expectation: Calling an arrow function with another object does not replace its lexical this.
const order = {
customer: "Priya"
};
const showCustomer = () => {
console.log(this.customer);
};
showCustomer.call(order);
Correct: Use a normal function when you need a dynamic this value.
function showCustomer() {
console.log(this.customer);
}
showCustomer.call(order);
Rule: Use normal functions when call(), apply() or bind() must control this.
Mistake 4: Assuming bind() can be changed later
A bound function keeps its original this binding. Calling it with another object does not replace that binding.
const priyaOrder = { customer: "Priya" };
const raviOrder = { customer: "Ravi" };
function showCustomer() {
console.log(this.customer);
}
const showPriya = showCustomer.bind(priyaOrder);
showPriya.call(raviOrder);
Expected output
Priya
Rule: A later call() cannot replace a bound function's this value.
Best practices
- Use
call()when you need an immediate call with separate arguments. - Use
apply()when your arguments already exist in an array. - Use
bind()when you need a function for later execution. - Prefer arrow functions for callbacks that should keep the surrounding
this. - Prefer the spread operator for many modern array-to-argument use cases.
- Avoid binding the same function repeatedly when one bound function can be reused.
8. What Happens Internally?
All three methods are available on JavaScript functions through Function.prototype.
Function.prototype = the shared prototype that provides methods to JavaScript functions.
When you call call() or apply(), JavaScript invokes the target function with the supplied context and arguments.
When you call bind(), JavaScript creates a new bound function. That function remembers its bound context and any saved arguments.
Important details about this
- In a normal function,
thisdepends on how the function is called. - In strict mode, a plain function call does not replace an undefined
thiswith the global object. - In non-strict mode, a plain function call may substitute the global object for an undefined or null context.
- Arrow functions use the surrounding lexical
this. - A bound function keeps its bound context when called later.
Important: These methods do not change an object's stored data. They control the function call's context and arguments.
9. How This Comes Up in Interviews
Question 1: What is the difference between call(), apply() and bind()?
What the interviewer wants: They want to check your understanding of function execution and arguments.
Strong answer:
"All three methods help us control the this value of a normal function. Call and apply execute the function immediately. Call accepts arguments separately, while apply accepts an array or array-like argument list. Bind returns a new function that we can execute later."
Question 2: Does bind() execute a function?
Strong answer:
"No. Bind returns a new function with a fixed this value. We can store that function and execute it later. We can also pass some arguments while creating the bound function."
Question 3: Can we use call() with an arrow function?
Strong answer:
"We can call the method, but it cannot replace an arrow function's this value. Arrow functions get this from their surrounding scope. I use a normal function when I need a dynamic this value."
Question 4: What is function borrowing?
Strong answer:
"Function borrowing means using an existing function with another object. For example, I can use the restaurant receipt function for Priya's order and Ravi's order. I do not need to copy the receipt logic into both objects."
What interviewers expect at different levels
| Level | Expected understanding |
|---|---|
| Junior developer | Explain the syntax and main differences. |
| Senior developer | Explain function borrowing, callbacks and arrow function behavior. |
| Technical architect | Explain context behavior, maintainability and appropriate use in application design. |
10. Hands-On Practice
Beginner: Restaurant receipt
Create a function named showBill(). It should display a customer's name and total amount.
- Create two order objects for Priya and Ravi.
- Use
call()to print Priya's bill. - Use
apply()to print Ravi's bill.
Expected result: Both bills should display the correct customer and total.
Intermediate: Prepare a receipt function
Create a receipt function with currency and message arguments.
- Use
bind()to fix the order object. - Save the returned function in a variable.
- Call it later with the remaining message argument.
Hint: Remember that bind() returns a new function.
Advanced: Build a reusable discount calculator
Create a function that calculates a restaurant order's discount.
- Store the order total and discount percentage in an object.
- Use a normal function that reads values through
this. - Use
call()with one order. - Use
apply()with arguments stored in an array. - Use
bind()to prepare a calculator for later execution.
Expected result: One calculator function should work with different orders.
11. Frequently Asked Questions
Can we use call(), apply() and bind() with any function?
They are methods available on JavaScript functions. They work with normal functions and many built-in functions. However, they cannot change an arrow function's lexical this.
Does apply() always perform better than call()?
No. Choose based on how your arguments are stored. Do not assume one is always faster.
Can bind() accept arguments?
Yes. You can pass arguments while calling bind(). These values are saved and placed before arguments supplied during the later function call.
Can we bind a function more than once?
Yes. But a later bind does not replace the first bound this value. Any new arguments are added after the arguments already saved.
What is the modern alternative to apply() with arrays?
The spread operator is often simpler. For example, use Math.max(...menuPrices) to pass array values as separate arguments.
12. Next Steps and Community
Explore further
- MDN: Function.prototype.call()
- MDN: Function.prototype.apply()
- MDN: Function.prototype.bind()
- MDN: JavaScript this keyword
Share your knowledge
Try the restaurant examples and create your own variations. Share your code and explain which method you selected. You can also discuss other ways to solve the same problem.
Keep this simple rule in mind: call runs now, apply runs now with an array, and bind prepares a function for later.