JavaScript bind(): Bound Receivers and Arguments
Quick answer
bind creates a new callable that stores a target function, a receiver and optionally leading arguments. Calling it later forwards the combined arguments.
How it works
fn.bind(obj, a) does not call fn immediately. The resulting function supplies obj and prepends a to later arguments. Rebinding it cannot replace the original bound receiver for ordinary calls, although it can add arguments. If the target is constructible, a bound function can also be constructed; construction ignores the bound this. That constructor behavior is covered in the next class chapter.
function total(a, b) { return this.base + a + b; }const add = total.bind({base: 10}, 3);console.log(add(4));Expected output in Node.js:
17The bound receiver and leading argument are supplied before the new argument.
function read() { return this.n; }const a = read.bind({n: 2}); const b = a.bind({n: 8});console.log(a(), b());Expected output in Node.js:
2 2The second bind cannot replace the earlier receiver.
Common mistakes
Calling bind without storing its returned function leaves the original callable unchanged.
Practice
Create a bound price function with an initial quantity and compare two fee inputs.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
JavaScript bind(): Bound Receivers and Arguments — Knowledge Check
Apply the taught concept and predict the output.
What does bind return?
Where do bound positional arguments appear?
Can a second bind replace an ordinary bound call receiver?
What is the exact console output in JavaScript bind(): Bound Receivers and Arguments example 1?
function total(a, b) { return this.base + a + b; }const add = total.bind({base: 10}, 3);console.log(add(4));What is the exact console output in JavaScript bind(): Bound Receivers and Arguments example 2?
function read() { return this.n; }const a = read.bind({n: 2}); const b = a.bind({n: 8});console.log(a(), b());