# JavaScript Function methods  - call(), apply(), bind()

Every function in JavaScript is a **Function object**.

<details data-node-type="hn-details-summary"><summary>Function object</summary><div data-type="detailsContent">In essense, functions themselves are <strong>objects with callable behavior</strong>. Because functions are <code>Function</code> objects, they come equipped with powerful built-in methods such as <code>call()</code>, <code>apply()</code>, and <code>bind()</code>— each of which lets you explicitly control how and where a function is executed, and more importantly, what <code>this</code> refers to within it.</div></details>

We shall discuss three of its methods -

* *call()*
    
* *apply()*
    
* *bind()*
    

## *call()*

Syntax

```javascript
call(thisArg, arg1, ... , argN)
```

#### Parameters

`thisArg`: Object to be used as current object

`arg1, ... , argN`: Comma-separated list of arguments to be passed to the function

#### Example

Let's dissect the `call()` method using a simple example.

Consider an object `person` that has a property `name` and a method `feedDog`

![Generated image](https://sdmntprcentralus.oaiusercontent.com/files/00000000-3eb8-61f5-b171-5a5b8aa231fd/raw?se=2025-10-11T05%3A48%3A33Z&sp=r&sv=2024-08-04&sr=b&scid=2782b823-d84d-595c-b59f-0610c7715530&skoid=03727f49-62d3-42ac-8350-1c0e6559d238&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2025-10-10T21%3A47%3A47Z&ske=2025-10-11T21%3A47%3A47Z&sks=b&skv=2024-08-04&sig=NCJcgfSKPlTYCuTC8mgVP1g1sdoz7hfNYNlPCOuwv/o%3D align="center")

```javascript
const person = {
   name: "Sally",
   feedDog() {
       console.log(this.name, "feeds the dog");
   }
}
```

Consider another object `neighbor` with a property `name`. Note that neighbor does not feed the dog and hence, it has no method called `feedDog`

```javascript
const neighbor = {
    name: "Jon",
}
```

Now, let's say on a certain day `person` is not available and so asks her neighbor to feed the dog.

`neighbor` has no method called `feedDog` so it borrows `person`'s `feedDog` method

```javascript
person.feedDog.call(neighbour)
// Jon feeds the dog
```

The above line of code *calls* `person.feedDog` on the the neighbor object. So `this` now refers to the neighbor object so this.name resolves to Jon

The params following `thisArg` are arguments to the function.

Let's modify the person.feedDog to accept two arguments:

```javascript
const person = {
   name: "Sally",
   feedDog(numberOfBowls) {
       console.log(this.name, "feeds the dog", foodItem, "at", time);
   }
}
```

```javascript
person.feedDog.call(neighbour, "chimken", "12 noon")
// Jon feeds the dog chimken at 12 noon
```

In summary, the `call()` method lets you **borrow functions** from one object and use them with another by explicitly setting what `this` refers to.  
It executes the function **immediately** and accepts arguments **individually** rather than as an array.

---

## *apply()*

The `apply()` method is similar to `call()`, except that it takes **arguments as an array (or array-like object)** rather than a comma-separated list.

**Syntax**

```javascript
apply(thisArg, [argsArray])
```

**Parameters**

* `thisArg`: Object to use as the current object (`this`) inside the function.
    
* `argsArray`: Array (or array-like object such as `arguments`) containing arguments to pass to the function.
    

**Example**

Let’s use the same setup:

```javascript
const person = {
   name: "Sally",
   feedDog(foodItem, time) {
       console.log(this.name, "feeds the dog", foodItem, "at", time);
   }
}

const neighbor = {
   name: "Jon"
};
```

Now, instead of calling with individual arguments, we’ll use `apply()` with an array:

```javascript
person.feedDog.apply(neighbor, ["chimken", "12 noon"]);
// Jon feeds the dog chimken at 12 noon
```

The result is **identical** to `call()`, but the **way arguments are passed** is different.

**When to use** `apply()`

* When your arguments are already in an array form.
    
* When you don’t want to manually unpack or spread them.
    

A common use case is when **using built-in functions like** `Math.max`:

```javascript
const numbers = [3, 5, 8, 2];
const max = Math.max.apply(null, numbers);
console.log(max); // 8
```

---

## ***bind()***

While `call()` and `apply()` **invoke** the function immediately, `bind()` **returns a new function** with a permanently bound `this` value.

**Syntax**

```javascript
bind(thisArg, arg1, ..., argN)
```

**Parameters**

* `thisArg`: The object to be used as `this` when the function is called.
    
* `arg1, ..., argN`: Optional arguments to pre-set (partial application).
    

**Example**

```javascript
const person = {
   name: "Sally",
   feedDog(foodItem, time) {
       console.log(this.name, "feeds the dog", foodItem, "at", time);
   }
}

const neighbor = { name: "Jon" };

// Bind returns a new function
const feedByNeighbor = person.feedDog.bind(neighbor, "chimken", "12 noon");

// The function isn’t executed yet
feedByNeighbor(); // Jon feeds the dog chimken at 12 noon
```

The `feedByNeighbor` function retains the context of `neighbor` no matter where it is called.

This is especially useful when passing methods as **callbacks or event handlers**, where `this` may otherwise get lost.

**Example:**

```javascript
const button = document.querySelector("button");
button.addEventListener("click", person.feedDog.bind(person, "kibble", "7 am"));
```

Without `bind()`, `this` would refer to the button element instead of `person`.

---

## Summary: `call()` vs `apply()` vs `bind()`

All three methods—`call()`, `apply()`, and `bind()`—allow you to **manually control the value of** `this` in JavaScript.

* Use `call()` when you know the exact arguments.
    
* Use `apply()` when arguments are in an array.
    
* Use `bind()` when you need to preserve context for later execution.
    

| Method | Invokes Immediately | Arguments Format | Returns a Function | Common Use Case |
| --- | --- | --- | --- | --- |
| **call()** | Yes | Comma-separated | No | Direct invocation with known args |
| **apply()** | Yes | Array or array-like | No | Invoking with dynamic/array args |
| **bind()** | No | Comma-separated | Yes | Preserving `this` for later calls |

![](https://sdmntprnortheu.oaiusercontent.com/files/00000000-acd8-61f4-9d3e-fab89907f469/raw?se=2025-10-11T06%3A03%3A22Z&sp=r&sv=2024-08-04&sr=b&scid=231b637c-4ce8-5143-8ea4-2748c1d5a3d3&skoid=03727f49-62d3-42ac-8350-1c0e6559d238&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2025-10-10T23%3A07%3A21Z&ske=2025-10-11T23%3A07%3A21Z&sks=b&skv=2024-08-04&sig=OfBtU0Wj%2BU95I7gnMQSJDXbKIzk/vZ/RthN/9yVGijY%3D align="left")
