By the end, you can explicitly set this for a JavaScript function using call, apply, or bind, and choose the right one for immediate calls, array arguments, or reusable functions.
What it is
In JavaScript, this is determined by how a function is called, not where it is defined. call, apply, and bind let you control that value explicitly. They are methods on Function.prototype.
call invokes a function immediately with a chosen this and individual arguments. apply invokes immediately with a chosen this and an array-like list of arguments. bind returns a new function with a chosen this, and optional preset arguments, that can be called later.
Related terms: execution context, this binding, first-class functions, higher-order functions.
Why it matters
- Reuse object methods on other objects without copying code.
- Pass functions as callbacks while preserving the intended
this. - Convert array-like arguments into function parameters, such as
Math.max.apply(null, numbers). - Create reusable event handlers or partial applications.
- Make code clearer when
thiswould otherwise be lost.
Syntax or steps
- Choose the function you want to run.
- Choose the object that should become
this. - Use
callfor immediate execution with separate arguments:fn.call(thisArg, arg1, arg2). - Use
applyfor immediate execution with an array-like argument list:fn.apply(thisArg, argsArray). - Use
bindwhen you need a new function to call later:const bound = fn.bind(thisArg, presetArg).
Example
const user = {
name: "Ada",
greet: function (greeting) {
return `${greeting}, ${this.name}!`;
}
};
const guest = { name: "Grace" };
// call: immediate, separate arguments
console.log(user.greet.call(guest, "Hello"));
// apply: immediate, arguments as array
console.log(user.greet.apply(guest, ["Hi"]));
// bind: returns a reusable function
const bound = user.greet.bind(guest);
console.log(bound("Welcome"));
// bind with preset arguments
const welcomeGuest = user.greet.bind(guest, "Welcome");
console.log(welcomeGuest());
user.greet.call(guest, "Hello")runsgreetimmediately withthisset toguest.user.greet.apply(guest, ["Hi"])does the same, but passes arguments as an array.const bound = user.greet.bind(guest)creates a new function withthisfixed toguest.bound("Welcome")calls the saved function later.user.greet.bind(guest, "Welcome")presets the first argument, sowelcomeGuest()needs no arguments.
Common mistakes
- Forgetting that
binddoes not call the function. It returns a new function; you must call it later. - Using
applywith separate arguments instead of an array-like list. - Assuming
thisinside a bound function can be changed bycallorapply. Once bound, the first argument tobindis fixed unless the function is used as a constructor. - Using
callorapplyon arrow functions expecting to changethis. Arrow functions do not have their ownthis; they inherit it from the surrounding scope.
When to use it
Use call when you need to invoke a function now with a specific this and normal arguments. Use apply when the arguments are already in an array or array-like object. Use bind when you need to save a function with a fixed this for later, such as a callback or event handler.
| Method | Returns | Arguments | Best for |
|---|---|---|---|
call |
Function result | Individual arguments | Immediate reuse of a method |
apply |
Function result | Array-like arguments | Passing arrays to functions like Math.max |
bind |
New function | Individual preset arguments | Callbacks, event handlers, partial application |
Practice
Guided exercise: Create an object team with a method announce that returns this.name and a message. Use call to run it with a different object.
const team = {
name: "Blue",
announce: function (message) {
return `${this.name}: ${message}`;
}
};
const other = { name: "Red" };
console.log(team.announce.call(other, "Ready"));
Expected output: Red: Ready.
Challenge: Use bind to create a reusable function that always announces for other with the preset message Go.
Solution hint: const go = team.announce.bind(other, "Go"); console.log(go());
Quick check
Question: Which method should you use if you already have arguments in an array and want to call the function immediately?
Answer: apply.
Summary
call, apply, and bind are tools for explicit this control. call and apply run a function now, while bind creates a reusable function with a fixed this. Choose based on whether you need immediate execution, array arguments, or a saved callback.