JavaScript Notes
Master JavaScript bind() with permanent this binding, partial application, event handlers, currying patterns, common mistakes, and top interview Q&A.
What is bind()?
bind() is a Function.prototype method that creates a brand new function with this permanently set to the value you specify. Unlike call() and apply(), bind does not execute the function immediately — it returns a new reusable function that you can call whenever you need.
One-liner:bind()saves a customthis(and optional pre-filled arguments) into a new function for later use.
Syntax
const boundFn = functionName.bind(thisArg, ...presetArgs);| Parameter | Meaning |
|---|---|
thisArg | The value permanently used as this in the bound function |
...presetArgs | Optional leading arguments pre-filled in the new function |
Return value: A new function — the original is unchanged. Calling the new function executes the original with the bound this and preset args.
How bind() Works Internally
Basic Example — Step by Step
const user = { name: "Maya" };
function show() {
return `Hello, ${this.name}!`;
}
const bound = show.bind(user);
console.log(bound());Hello, Maya!
Execution trace:
Example 2 — bind() with Preset Arguments (Partial Application)
bind() can pre-fill one or more arguments, creating a specialized version of a function.
function greet(greeting, punctuation) {
return `${greeting}, ${this.name}${punctuation}`;
}
const user = { name: "Priya" };
// Pre-fill greeting = "Hello"
const sayHello = greet.bind(user, "Hello");
console.log(sayHello("!"));
console.log(sayHello("?"));Hello, Priya! Hello, Priya?
Only punctuation needs to be supplied when calling sayHello — greeting is already locked in.
Example 3 — bind() in Event Handlers
The most common real-world use of bind():
Count: 1 Count: 2 Count: 3 ...
Without .bind(this), setInterval calls tick() with this = undefined (strict) or window (non-strict), breaking this.count.
Example 4 — Function Borrowing with bind()
const student = { name: "Aman", grade: "A" };
const teacher = {
name: "Neha",
report(subject) {
return `${this.name} — ${subject} — Grade: ${this.grade}`;
},
};
const studentReport = teacher.report.bind(student);
console.log(studentReport("Mathematics"));Aman — Mathematics — Grade: A
Example 5 — bind() in React (Class Components)
class Button extends React.Component {
constructor(props) {
super(props);
// Bind in constructor so `this` is correct in handleClick
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
console.log(`Button clicked by: ${this.props.label}`);
}
render() {
return <button onClick={this.handleClick}>Click</button>;
}
}Button clicked by: Submit
Scope / Execution Context Diagram
Comparison Table — call() vs apply() vs bind()
| Feature | call() | apply() | bind() |
|---|---|---|---|
| Executes immediately | ✅ Yes | ✅ Yes | ❌ No (returns fn) |
| Argument format | Individual | Array | Individual + preset |
| Returns | Function result | Function result | New bound function |
this change permanent | ❌ No | ❌ No | ✅ Yes (new fn) |
| Re-bindable | N/A | N/A | ❌ No |
| Works on arrow functions | ❌ No | ❌ No | ❌ No |
| Best for | One-time call | Array args one-time | Callbacks / event handlers |
Common Mistakes
❌ Mistake 1 — Calling bind() in a Render Loop
// WRONG — creates a new function on every render
<button onClick={this.handleClick.bind(this)}>Click</button>
// CORRECT — bind once in constructor
this.handleClick = this.handleClick.bind(this);Each bind() call creates a new function object. Doing it in a render loop is a memory/performance issue.
❌ Mistake 2 — Expecting bind() to Run Immediately
[Function: bound greet]
// CORRECT
const result = greet.bind(user)(); // Call it!
console.log(result);Hi, Raj
❌ Mistake 3 — Trying to Rebind a Bound Function
function show() { return this.name; }
const userA = { name: "Alice" };
const userB = { name: "Bob" };
const bound = show.bind(userA);
const rebound = bound.bind(userB); // Attempt to rebind
console.log(rebound());Alice
Once bound, this cannot be changed — even with another bind(), call(), or apply().
❌ Mistake 4 — Using bind() on Arrow Functions
undefined
Arrow functions capture this lexically. bind() has no effect on their this.
Edge Cases
Edge Case 1 — bind() with new Operator
When a bound function is used with new, the bound thisArg is ignored — this becomes the newly created object:
function Person(name) {
this.name = name;
}
const BoundPerson = Person.bind({ name: "ignored" });
const p = new BoundPerson("Actual Name");
console.log(p.name);Actual Name
Edge Case 2 — bind() Preserves Function Length Correctly
function add(a, b, c) { return a + b + c; }
const addFive = add.bind(null, 5);
console.log(addFive.length); // Remaining params: 3 - 1 = 2
console.log(addFive(3, 2));2 10
Real World Use Cases 🔧
| Use Case | Example |
|---|---|
| Event handlers in classes | this.handleClick.bind(this) |
| React class component methods | Binding in constructor |
| Partial application / currying | Pre-fill common arguments |
| Callback with fixed context | arr.forEach(fn.bind(ctx)) |
| Function borrowing (reusable) | obj.method.bind(other) |
setTimeout with correct this | setTimeout(fn.bind(this), 1000) |
Interview Questions 🎯
Q1. What does bind() return?
bind() returns a new function (a bound function). It does not execute the original function. The new function, when called, executes the original with the pre-set this and any pre-filled arguments.
Q2. What is the difference between bind() and call()?
call()executes immediately and returns the function's resultbind()returns a new function without executing; you call it later
show.call(user); // runs now, returns result
const fn = show.bind(user); // returns new fn, runs on fn()Q3. Can you rebind a bound function?
No. Once a function is bound with bind(), the this is permanently locked. Any further bind(), call(), or apply() calls on the bound function cannot change its this.
Q4. Does bind() work with arrow functions?
bind() cannot change this for arrow functions. Arrow functions capture this from their enclosing lexical scope at definition time, and no method can override it.
Q5. What happens when a bound function is used with new?
When a bound function is called with new, the new operator overrides the bound this. The new function's this is set to the newly created object, ignoring the bound thisArg.
Q6. What is partial application with bind()?
Partial application means pre-filling some arguments of a function to create a more specific version:
function multiply(a, b) { return a * b; }
const double = multiply.bind(null, 2); // a = 2 is pre-filled
console.log(double(5)); // b = 5 → 2 * 5 = 1010
Q7. What is the performance concern with bind() in React?
Calling bind() in JSX (e.g., onClick={this.fn.bind(this)}) creates a new function on every render, wasting memory and breaking reference equality checks in shouldComponentUpdate / React.memo.
Q8. How is bind() different from arrow functions in class properties?
Both solve the same problem. Arrow class fields are more concise.
Key Takeaways 🔑
bind()does not execute the function — it returns a new bound function- The bound function's
thisis permanently locked — nocall/apply/bindcan change it bind()supports partial application — pre-fill leading arguments- Arrow functions ignore
bind()forthis— lexicalthisalways wins - With
new, the boundthisis overridden by the newly created object - Main real-world use: fixing
thisin callbacks and event handlers - Avoid calling
bind()in loops/renders — it creates a new function each time
Practice Problems
- Create a
greet(greeting)function and bind it to auserobject. Call it with different greetings. - Write a
multiply(a, b)function and usebind()to createtriple(always multiplies by 3). - Simulate a timer class where
this.countincrements every second usingbind()insidesetInterval. - Demonstrate that a bound function cannot be rebound using a code example.
- Write a polyfill for
Function.prototype.bindusing closures.
Summary
bind() is a Function.prototype method that creates a new permanently-bound function with a fixed this and optionally pre-filled arguments. Unlike call() and apply(), it does not execute immediately — the returned function can be stored and called any number of times later. Key rules: the binding is permanent (cannot be overridden by further bind/call/apply), arrow functions are immune to bind() for this, and when used with new, the bound this is ignored. bind() shines in event handlers, callbacks, React class components, and partial application patterns.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for JavaScript bind() Method - Complete Guide with Examples.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this JavaScript Master Course topic.
Search Terms
javascript-complete-guide, javascript master course, javascript, complete, guide, advanced, bind, javascript bind() method - complete guide with examples
Related JavaScript Master Course Topics