πŸ”— Understanding call, apply, and bind in JavaScript (Beginner to Advanced with React Use Cases)

krishankrishan
3 min read

JavaScript gives us powerful tools to control this context using .call(), .apply(), and .bind(). Understanding these not only helps with core JS logic but also becomes essential when diving deeper into React.


πŸš€ Step 1: The Basics β€” What is this?

In JavaScript, this refers to the object that is executing the current function.

const user = {
  name: 'Alice',
  greet() {
    console.log(`Hello, ${this.name}`);
  },
};

user.greet(); // Hello, Alice

But what if we extract the function?

const greetFn = user.greet;
greetFn(); // Hello, undefined (or global object)

Here, this lost its context. That’s where call, apply, and bind step in.


πŸ§ͺ Step 2: .call() β€” Invoke Immediately with Custom this

call() allows you to invoke a function immediately, passing a specific this.

function greet(role) {
  console.log(`${this.name} is a ${role}`);
}

const user = { name: 'Bob' };
greet.call(user, 'Developer'); // Bob is a Developer

βœ… Use call() when you want to execute a function right away with a given context.


βš™οΈ Step 3: .apply() β€” Just Like call, but Arguments as Array

greet.apply(user, ['Designer']); // Bob is a Designer

βœ… apply() is rarely used these days but handy when arguments are already in an array.


🧷 Step 4: .bind() β€” Don't Invoke Immediately

bind() returns a new function with the this context fixed.

const boundGreet = greet.bind(user, 'Tester');
boundGreet(); // Bob is a Tester

βœ… Useful when passing callbacks or event handlers in React!


βš›οΈ Step 5: React Real-World Usage

Let’s look at a React class component example:

class UserComponent extends React.Component {
  constructor() {
    super();
    this.state = { name: 'Charlie' };
    this.handleClick = this.handleClick.bind(this);
  }

  handleClick() {
    console.log(`${this.state.name} clicked!`);
  }

  render() {
    return <button onClick={this.handleClick}>Click Me</button>;
  }
}

Without bind, this.handleClick loses its context when passed as a callback.


πŸ’‘ React Functional Components?

With arrow functions and hooks, we rarely use call, apply, or bind. But understanding them explains why arrow functions maintain context and why closures in hooks work the way they do.


🎯 Interview Tip:

Be ready to answer:

  • What's the difference between call, apply, and bind?

  • When would you use bind in React?

  • Why might .bind() hurt performance in JSX render?


πŸ“¦ Summary Table

MethodInvokes ImmediatelyArgs FormatReturns FunctionUse Case
callβœ…Comma-separated❌Direct function call with custom this
applyβœ…Array❌Like call, but for array args
bind❌Comma-separatedβœ…Callback or event handler binding

✍️ Written by Krishan β€” follow me on Twitter and GitHub and Linkedin

πŸ’‘ Check out the current code examples and notes repo: GitHub

πŸ“ Read more on my blog: Hashnode

0
Subscribe to my newsletter

Read articles from krishan directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

krishan
krishan

πŸ‘‹ Hey, I'm a Frontend Developer passionate about building clean, user-friendly interfaces. πŸš€ Learning and sharing everything from React, JavaScript, HTML/CSS to advanced topics like Data Structures, Algorithms & System Design. πŸ’» Documenting my journey from fundamentals to becoming a top-tier developer β€” one blog at a time. πŸ“š Join me as I break down complex topics into easy, practical lessons β€” with GitHub repos, code snippets, and real-world examples. πŸ” Consistent growth, community learning, and aiming high!