JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionsjavascript
PrevNext

Learn the concept

The this Keyword

javascript
mid
this

What are call, apply, and bind and how do they differ?

call
apply
bind
this
context
method-borrowing
partial-application
Quick Answer

call, apply, and bind are Function.prototype methods that explicitly set the `this` context. call and apply invoke the function immediately (call takes individual arguments, apply takes an array), while bind returns a new function with `this` permanently bound.

Detailed Explanation

Function.prototype.call(thisArg, ...args)

  • Invokes the function immediately
  • First argument sets this
  • Remaining arguments passed individually
  • Returns the function's return value

Function.prototype.apply(thisArg, [argsArray])

  • Invokes the function immediately
  • First argument sets this
  • Second argument is an array (or array-like) of arguments
  • Returns the function's return value
  • Before ES6 spread, this was the primary way to pass arrays as arguments

Function.prototype.bind(thisArg, ...args)

  • Does NOT invoke the function
  • Returns a new function with this permanently bound
  • Supports partial application — pre-fill arguments
  • The bound function's this cannot be overridden by call/apply/new

Key Differences:

| Method | Invokes immediately? | Arguments format | Returns | |--------|---------------------|------------------|---------| | call | Yes | Individual | Result | | apply | Yes | Array | Result | | bind | No | Individual | New fn |

Arrow Functions:

  • Arrow functions do NOT have their own this
  • call, apply, and bind have no effect on an arrow function's this
  • They inherit this from their enclosing lexical scope

Common Patterns:

  • Method borrowing: Use methods from one object on another
  • Partial application: Pre-fill some arguments with bind
  • Event handlers: Bind class methods for React event handlers (pre-hooks era)
  • Variadic functions: Use apply to spread arguments (now replaced by spread syntax)

Code Examples

call vs apply vs bindJavaScript
const user = {
  name: 'Alice',
  greet(greeting, punctuation) {
    return `${greeting}, I'm ${this.name}${punctuation}`;
  }
};

const bob = { name: 'Bob' };

// call — individual arguments
user.greet.call(bob, 'Hello', '!');
// "Hello, I'm Bob!"

// apply — array of arguments
user.greet.apply(bob, ['Hi', '?']);
// "Hi, I'm Bob?"

// bind — returns a new function
const bobGreet = user.greet.bind(bob, 'Hey');
bobGreet('.');
// "Hey, I'm Bob."

// bind is permanent — call/apply can't override it
bobGreet.call({ name: 'Charlie' }, '!');
// "Hey, I'm Bob!" — still Bob

Real-World Applications

Use Cases

React Class Components

Before hooks, bind was essential in constructors to preserve 'this' context in event handlers passed as props

Utility Libraries

Libraries like Lodash use call/apply internally for method borrowing and to handle variadic functions across different contexts

Function Composition

Partial application with bind enables creating specialized functions from general-purpose ones without wrapper functions

Mini Projects

Custom Bind Implementation

intermediate

Implement Function.prototype.myBind from scratch supporting partial application and new operator

Method Borrowing Utility

beginner

Build a utility that borrows methods from one prototype to enhance array-like objects

Industry Examples

React

Class component patterns relied heavily on bind for event handlers before hooks eliminated the need

Lodash

Uses apply and call internally for functions like _.invoke and _.partial to handle flexible argument passing

Resources

MDN - Function.prototype.call()

docs

MDN - Function.prototype.apply()

docs

MDN - Function.prototype.bind()

docs

Related Questions

How does the 'this' keyword work in JavaScript?

mid
this

What is a closure in JavaScript and why are they useful?

mid
closures
Previous
When should you use Map and Set instead of plain objects and arrays?
Next
What is the difference between block scope and function scope?
PrevNext