Skip to content
FunctionsChapter 18 / 47Intermediate5 min read

call, apply & bind

Learn how to set a function's this yourself with JavaScript call, apply and bind, the difference between the three, partial application and real use cases.

On this page

1. What are call, apply and bind?

Normally this is decided by the call site. These three methods override that rule. call and apply run the function immediately, while bind returns a new function that will run later.

2. Why does it matter?

Sometimes you have to pass a method on its own, away from its object (callbacks, timers), and this is lost. Other times you need to run one object's method on another object.

Without explicit bindingWith call, apply, bind
this is undefined in a callbackbind keeps this fixed
A method does not run on another objectcall runs it on any object
Hard to spread an array into separate argumentsapply turns an array into arguments
Passing the same arguments again and againbind fixes some arguments in advance

3. Where is it used?

SituationMethod
Passing a method to a callback or timerbind
Running one object's method on another objectcall
When the arguments are in an arrayapply (or modern spread)
Fixing some arguments in advancebind (partial application)
Library or polyfill codecall and apply

4. How does it work?

MethodSyntaxWhen it runsReturns
callfn.call(obj, a, b)ImmediatelyThe function's result
applyfn.apply(obj, [a, b])ImmediatelyThe function's result
bindfn.bind(obj, a)LaterA new bound function

A trick to remember: call = arguments with commas, apply = arguments in an array, bind = tie it up and keep it for later.

Flow of bind
The original function
bind(obj, the first arguments)
You get a new function, this is always obj
Called later with the remaining arguments
The original function runs, this = obj

5. Code example

The basic difference between the three

js
function introduce(city, role) {
  return `${this.name} from ${city}, ${role}`;
}

const ali = { name: "Ali" };
const sara = { name: "Sara" };

console.log(introduce.call(ali, "Lahore", "developer"));
console.log(introduce.apply(sara, ["Karachi", "designer"]));

const introduceAli = introduce.bind(ali, "Islamabad");
console.log(introduceAli("student"));

Saving this in a callback

js
const logger = {
  prefix: "[app]",
  log(message) {
    console.log(`${this.prefix} ${message}`);
  },
};

const messages = ["started", "ready"];
messages.forEach(logger.log.bind(logger));

const scores = [40, 85, 72];
console.log(Math.max.apply(null, scores));
console.log(Math.max(...scores));

How does the code work?

ItemMeaning
introduce.call(ali, ...)this = ali, arguments with commas
introduce.apply(sara, [...])this = sara, arguments in an array
bind(ali, "Islamabad")this and city are fixed, a new function
introduceAli("student")Only the remaining argument role is given
logger.log.bind(logger)this.prefix works inside forEach too
Math.max(...scores)Spread, the modern alternative to apply

Interview tip

You cannot change this of a bound function by calling bind or call on it again, the first bind stays. And the this part of call, apply and bind has no effect on arrow functions, because they have no this of their own.

6. Common mistakes

Mistake: Thinking bind is the same as call

Why? bind does not run the function, it only gives a new function. You get a function instead of a result.

Better: Use call or apply when you want the result right away, and bind when you want to run it later.

Mistake: Trying to change this on an arrow function with bind

Why? An arrow's this is lexical, and the first argument of bind is ignored.

Better: If you need to change this, use a normal function or a method.

Mistake: Creating a new bind on every render or loop

Why? A new function is made every time, and removeEventListener does not work because the reference is different.

Better: Create the bound function once, keep it in a variable and use the same one in add and remove.

Mistake: Pushing apply instead of spread

Why? In modern code, fn.apply(null, arr) is harder to read.

Better: When you do not need this, write fn(...arr). Use apply only when you need an array together with this.

7. What to learn before and after

Suggested order
The this Keyword
call, apply & bind
Higher-Order Functions
Classes

Final quick revision

PointDetail
callSets this, comma arguments, runs immediately
applySets this, array arguments, runs immediately
bindA new function, this fixed permanently
Partial applicationFix the first arguments with bind
Arrow functionsthis does not change