Skip to content
jshacks.dev
Fundamentals

JavaScript Interview Question Explain call apply and bind

How to answer call, apply and bind: call and apply invoke with a chosen this, bind returns a new function. See borrowing methods and a hand-written bind.

8 min read
Summarize in AI
Flat illustration of a service container binding interfaces to classes, with the JavaScript, Vite and npm logos.

Three methods on every function, call, apply and bind, let you decide what this is for a function you did not write. The interview question about them looks like three definitions, and in practice the interviewer is checking four things: that you know the difference between invoking now and creating a function for later, that you know how arguments are passed, that you can name a real use, and that you can write one of them yourself. This post gives the answer in thirty seconds, then each method with exact output, the idiom of borrowing methods, partial application, the hand-written bind, the limits of all three, and the wrong answers to avoid. The examples run on Node.js 24 as ES modules, so code is strict.

The answer in thirty seconds

All three set the value of this for a function. call invokes the function immediately, with this as the first argument and the other arguments listed one by one. apply also invokes immediately, with this first and the arguments passed as a single array. bind does not invoke anything: it returns a new function with this fixed permanently, and optionally some leading arguments fixed too. A mnemonic: call takes a comma-separated list, apply takes an array, and bind binds for later.

JavaScript
function introduce(greeting, punctuation) {
  return `${greeting}, I am ${this.name}${punctuation}`;
}

const ada = { name: "Ada" };

console.log(introduce.call(ada, "Hello", "!"));
console.log(introduce.apply(ada, ["Hi", "."]));

const later = introduce.bind(ada, "Hey");
console.log(typeof later, later("?"));
Text
Hello, I am Ada!
Hi, I am Ada.
function Hey, I am Ada?

The first two lines call the function right away and differ only in how the arguments are given. The third creates later, a new function that has ada as this and "Hey" as its first argument, and calling it with the remaining argument produces the sentence. Writing this snippet and saying one sentence per line is a complete answer to the core of the question.

call and apply in detail

When there is no object to supply, the first argument is often null.

JavaScript
function add(a, b, c) {
  return a + b + c;
}

console.log(add.call(null, 1, 2, 3));
console.log(add.apply(null, [1, 2, 3]));
console.log(add(...[1, 2, 3]));

const numbers = [4, 9, 2];
console.log(Math.max.apply(null, numbers));
console.log(Math.max(...numbers));
Text
6
6
6
9
9

Math.max.apply(null, array) was the standard way to pass an array as arguments before spread syntax, and the spread form is now clearer. For apply, the second argument can also be an array-like object, such as { length: 2, 0: "a", 1: "b" }, and null or undefined means no arguments. Today, apply is mostly seen in older code, and in code that forwards arguments, as in fn.apply(this, arguments).

Borrowing methods

Because this is just a parameter, a method written for one kind of object can run on another, as long as the object has the shape the method expects.

JavaScript
const arrayLike = { 0: "a", 1: "b", 2: "c", length: 3 };

console.log(Array.prototype.map.call(arrayLike, (item) => item.toUpperCase()));
console.log(Array.prototype.slice.call(arrayLike, 1));
console.log(Array.prototype.join.call("hello", "-"));
console.log(Array.from(arrayLike));

const numbers = [10, 20];
const empty = [];
console.log(Array.prototype.push.apply(empty, numbers), empty);
Text
[ 'A', 'B', 'C' ]
[ 'b', 'c' ]
h-e-l-l-o
[ 'a', 'b', 'c' ]
2 [ 10, 20 ]

Array.prototype.map only needs this to have a length and indexed properties, so it works on an array-like object and even on a string. This was how code converted the old arguments object and DOM node lists into real arrays. Array.from() does that job today, and the borrowing idiom is still useful for generic methods. The last line also shows why apply was used to append one array to another: push takes any number of arguments, and apply supplies them from the array.

bind and partial application

bind returns a new function, and the extra arguments are fixed at the front.

JavaScript
function multiply(a, b) {
  return a * b;
}

const double = multiply.bind(null, 2);
const triple = multiply.bind(null, 3);

console.log(double(5), triple(5));
console.log([1, 2, 3].map(double));

console.log(multiply.name, multiply.length);
console.log(double.name, double.length);
Text
10 15
[ 2, 4, 6 ]
multiply 2
bound multiply 1

Fixing leading arguments is called partial application. The bound function gets the name bound multiply, which helps in stack traces, and its length drops by the number of arguments that were preset. In this form the this value is null because multiply does not use this. The more common use is fixing this for a method that will be called without its object, as in callbacks.

bind is permanent, and new beats it

JavaScript
function show() {
  return this.label;
}

const first = show.bind({ label: "first" });
const second = first.bind({ label: "second" });

console.log(first(), second());
console.log(first.call({ label: "call" }));
console.log(first.apply({ label: "apply" }));

function Thing() {
  this.label = "from constructor";
}

const BoundThing = Thing.bind({ label: "bound" });
console.log(new BoundThing().label);
Text
first first
first
first
from constructor

Binding a bound function again does nothing to this, and call and apply cannot override a bound this either. The one thing that does beat it is new: a bound function used as a constructor ignores the bound this and builds a fresh object, as the last line shows. Say this in the interview: the order of precedence is new, then bind, then call and apply, then a method call, then the default.

Arrow functions ignore all three

JavaScript
const outerThis = typeof this;

const arrow = () => typeof this;
const regular = function () {
  return typeof this;
};

console.log(outerThis, arrow(), arrow.call({}), arrow.bind({})());
console.log(regular(), regular.call({}), regular.call(5), regular.call("text"));
Text
undefined undefined undefined undefined
undefined object number string

An arrow function takes this from where it was written, which here is the module, where it is undefined, and no amount of call, apply or bind changes it. The second line shows a regular function in strict mode receiving exactly what is passed, including primitives: 5 stays a number. In sloppy mode, primitives passed as this are wrapped in objects, so the same call would print object. That is one more reason to write strict code.

Follow-up: write bind yourself

This is the most common implementation question about the three.

JavaScript
Function.prototype.myBind = function (thisArg, ...preset) {
  const original = this;

  function bound(...args) {
    const context = new.target ? this : thisArg;
    return original.apply(context, [...preset, ...args]);
  }

  bound.prototype = Object.create(original.prototype);
  return bound;
};

function Point(x, y) {
  this.x = x;
  this.y = y;
}

const withX = Point.myBind(null, 10);
const point = new withX(20);

console.log(point, point instanceof Point);

function sum(a, b) {
  return `${this.prefix}${a + b}`;
}

console.log(sum.myBind({ prefix: "total: " }, 1)(2));
Text
Point { x: 10, y: 20 } true
total: 3

The core is three lines: remember the function and the preset arguments, return a function that calls the original with the fixed this and the combined arguments. Two details make it correct. The new.target check lets the bound function work as a constructor, where it ignores thisArg, as the real one does. And setting the prototype keeps instanceof working. A shorter version without these two is acceptable as a first answer, and mentioning them earns the extra points.

JavaScript
Function.prototype.myCall = function (context, ...args) {
  const target = context === null || context === undefined ? globalThis : Object(context);
  const key = Symbol("fn");
  target[key] = this;
  const result = target[key](...args);
  delete target[key];
  return result;
};

function whoAmI(greeting) {
  return `${greeting} ${this.name}`;
}

console.log(whoAmI.myCall({ name: "Ada" }, "Hello"));
Text
Hello Ada

The myCall trick is to attach the function to the object under a unique key, call it as a method, so that this is the object, and remove it afterward. It is a teaching version: the real call also sets this to undefined in strict code instead of the global object. A Symbol key avoids clashing with the object's own properties.

Wrong answers that lose the point

  • "call and apply are the same thing." They differ in how arguments are passed, and that is the whole difference.

  • "bind calls the function." It returns a new function and calls nothing.

  • "bind modifies the original function." The original is unchanged.

  • "Arrow functions can be rebound with bind." Their this is fixed by where they are defined.

A mistake we hit: bind in a render path

JavaScript
class Widget {
  name = "widget";

  handler() {
    return this.name;
  }
}

const widget = new Widget();

const first = widget.handler.bind(widget);
const second = widget.handler.bind(widget);

console.log(first === second);
console.log(first() === second());

const listeners = new Set();
listeners.add(widget.handler.bind(widget));
console.log(listeners.delete(widget.handler.bind(widget)), listeners.size);
Text
false
true
false 1

Each bind call creates a new function, so two bound copies of the same method are different objects. Code that adds a listener with one bind and tries to remove it with another removes nothing, which the Set shows: delete returned false, and the listener is still there. We leaked event listeners this way in a component that bound its handler in two places. Bind once, store the result, and use that reference for adding and removing, or define the handler as an arrow class field.

Choosing between the methods

You wantUse
Call a function now with a chosen this and a list of argumentscall
Call it now when the arguments are in an arrayapply, or call with spread
A function that always has a chosen thisbind
A function with some arguments presetbind, or an arrow wrapper
A callback that needs the surrounding thisAn arrow function
To convert an array-like to an arrayArray.from()

Practical rules we follow

Prefer arrow functions and arrow class fields to bind for callbacks. Use call for method borrowing on generic methods. Use spread instead of apply for array arguments. Bind once and keep the reference if you need to remove it. Name preset functions clearly, since bound names show in stack traces. And when asked to write bind, handle new.target and the prototype.

Where the answer stops being enough

A deeper question covers Reflect.apply, which is the safer form because it does not depend on Function.prototype.apply being unmodified, the maximum number of arguments apply can pass, and how bind interacts with super and with classes. The answer above covers the working knowledge. The reference is in the MDN pages for Function.prototype.call, apply and bind.

Taggedinterview
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.