Course contentsShow
← JavaScript
Lesson 563 of 2,72314. The 'this' KeywordFree lesson

Partial Application with bind()

Passing arguments to bind() to pre-fill parameters and create partially applied functions.

Partial Application with bind()

What You'll Learn

You'll learn how to pass arguments to bind() to pre-fill some parameters, creating specialized versions of functions with fewer required arguments.

Beyond Just Fixing 'this'

We've seen that bind() locks in the this context, but it does something more: any additional arguments you pass to bind() get permanently "baked in" as the first arguments to the resulting function. This is called partial application—you're applying part of the arguments now, leaving the rest for later.

Think of it like a sandwich shop where you pre-order the bread and meat, then decide on toppings when you pick it up. You've "partially filled" your order.

How It Works

function greet(greeting, name) {
  console.log(greeting + ", " + name);
}

const sayHello = greet.bind(null, "Hello");
sayHello("Alice"); // "Hello, Alice"
sayHello("Bob");   // "Hello, Bob"

Here, bind(null, "Hello") creates a new function where greeting is always "Hello". The function still needs name, which you provide when calling sayHello().

Practical Use Case

function multiply(a, b) {
  return a * b;
}

const double = multiply.bind(null, 2);
const triple = multiply.bind(null, 3);

console.log(double(5));  // 10
console.log(triple(5));  // 15

You've created specialized versions of multiply without writing separate functions. The first argument (null) is the this value (irrelevant here), and 2 or 3 become permanent first parameters.

Arguments Fill Left-to-Right

Arguments passed to bind() always fill parameters from left to right—you can't skip the first parameter and fill the second.

Key Takeaway: Passing extra arguments to bind() creates a new function with those parameters pre-filled, letting you build specialized function variants from general-purpose ones.