Facebook Pixel

call, apply, and bind in JavaScript Explained

Three methods to set this explicitly. Here is how each works with examples.

call, apply, and bind in JavaScript Explained

call, apply, and bind let you set this explicitly when calling a function.

call

Invokes immediately with this and comma-separated arguments:

function greet(greeting, punctuation) { console.log(`${greeting}, ${this.name}${punctuation}`); } const user = { name: "Kunal" }; greet.call(user, "Hello", "!"); // "Hello, Kunal!"

apply

Same as call but arguments as an array:

greet.apply(user, ["Hi", "."]); // "Hi, Kunal."

bind

Returns a new function with this permanently bound:

const bound = greet.bind(user, "Hey"); bound("?"); // "Hey, Kunal?"

bind does not invoke immediately. The bound function can be called later. Re-binding has no effect.

Common Use Cases

  • Borrowing methods: Array.prototype.slice.call(arguments).
  • Fixing this in callbacks: setTimeout(obj.method.bind(obj), 1000).
  • Partial application: const add5 = add.bind(null, 5).

Arrow Functions Ignore Them

call, apply, and bind cannot change an arrow function's this.

The Takeaway

call: invoke with this and comma args. apply: same but array args. bind: return a new function with this bound. Use for borrowing methods, fixing this in callbacks, and partial application. Arrows ignore all three.

call and apply invoke immediately (call takes comma args, apply takes an array). bind returns a new function with this permanently bound, to be called later.

Use Array.prototype.slice.call(arguments) to borrow slice and convert the array-like arguments to a real array.

No. Arrow functions inherit this lexically. call, apply, and bind cannot change an arrow's this.

Presetting arguments: const add5 = add.bind(null, 5) creates a function where the first arg is always 5.

new > bind > call/apply > implicit (method call) > default (plain call). Arrow functions are an exception (lexical this).

Ready to master React completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.