Skip to main content

Command Palette

Search for a command to run...

Understanding this, call, apply, and bind in JavaScript

Updated
4 min read

One of the most confusing topics for beginners in JavaScript is the this keyword.

But the idea can actually be understood with a very simple rule:

this refers to the object that is calling the function.

You can think of it as answering the question:

“Who is calling this function?”


What Does this Mean in JavaScript?

The value of this depends on how a function is called.

Example:

function show() {
  console.log(this);
}

show();

Here, this refers to the global object (in browsers it is window).

The important point is:

this is determined by the caller of the function.


this Inside an Object

When a function is inside an object, this refers to that object.

Example:

let person = {
  name: "Gagan",
  greet: function() {
    console.log("Hello " + this.name);
  }
};

person.greet();

Output

Hello Gagan

Explanation:

  • The object person called the function

  • So this refers to person


Function Caller Relationship

https://cdn.hashnode.com/uploads/covers/6950e02f4d26437de06230fe/1bdbd917-9fec-41d4-a698-688a99af4ff0.png https://blog.openreplay.com/images/javascripts-this-explained-and-demystified/images/image03.png https://www.researchgate.net/publication/221562867/figure/fig3/AS%3A650031874990083%401531991084019/Basic-scheme-of-function-call-representation-Functions-are-lifted-to-the-KRL-where-they.png

4

Explanation:

Function → called by → object

The calling object becomes this


What Does call() Do?

call() allows you to manually set the value of this.

Example:

function greet() {
  console.log("Hello " + this.name);
}

let person = {
  name: "Gagan"
};

greet.call(person);

Output

Hello Gagan

Even though the function is not inside the object, we used call() to make this refer to person.


Passing Arguments with call()

Arguments are passed individually.

Example:

function introduce(city) {
  console.log(this.name + " from " + city);
}

let person = { name: "Gagan" };

introduce.call(person, "Delhi");

Output

Gagan from Delhi

What Does apply() Do?

apply() works the same as call().

The difference is how arguments are passed.

Arguments are passed as an array.

Example:

function introduce(city, country) {
  console.log(this.name + " from " + city + ", " + country);
}

let person = { name: "Gagan" };

introduce.apply(person, ["Delhi", "India"]);

Output

Gagan from Delhi, India

What Does bind() Do?

bind() creates a new function with a fixed this value.

It does not run immediately.

Example:

function greet() {
  console.log("Hello " + this.name);
}

let person = { name: "Gagan" };

let greetPerson = greet.bind(person);

greetPerson();

Output

Hello Gagan

Here:

  • bind() returns a new function

  • that function remembers the value of this


Difference Between call, apply, and bind

Method Execution Arguments
call() Executes immediately Passed individually
apply() Executes immediately Passed as array
bind() Returns new function Passed individually

Example summary:

func.call(obj, a, b)
func.apply(obj, [a, b])
func.bind(obj)

Visual Comparison

https://miro.medium.com/1%2A7Kt397qVGkLwjfFp0u91og.png https://i.sstatic.net/WHlX0.jpg https://miro.medium.com/v2/resize%3Afit%3A1400/1%2ARmoR4cauOc0IrW3TjWfUUA.png

4


Practical Example

let person = {
  name: "Rahul"
};

function greet(city) {
  console.log(this.name + " from " + city);
}

greet.call(person, "Mumbai");

Output

Rahul from Mumbai

Practice Assignment

1 Create an Object with a Method

Example:

let user = {
  name: "Amit",
  showName: function() {
    console.log(this.name);
  }
};

2 Borrow the Method Using call()

Example:

let anotherUser = {
  name: "Rohit"
};

user.showName.call(anotherUser);

3 Use apply() with Array Arguments

Example:

function introduce(city, country) {
  console.log(this.name + " from " + city + ", " + country);
}

let person = { name: "Amit" };

introduce.apply(person, ["Delhi", "India"]);

4 Use bind()

Example:

let boundFunction = introduce.bind(person);

boundFunction("Delhi", "India");