Javascript/call / apply / bind
Javascript / Topic

call / apply / bind

Learn how to explicitly control the value of 'this' using call(), apply(), and bind().

Difficulty

Intermediate

Read time

18 min

Format

Concept + Code

Focus

Interview ready
Concepts
call
apply
bind
this

01 / Foundation

Understand the concept

call(), apply(), and bind() allow you to explicitly specify what 'this' should refer to when invoking a function.

These methods are widely used for function borrowing, event handlers, partial application, and interview questions.

02 / Implementation

Difference Between call, apply and bind

call-apply-bind.js
const person = {
  name: "Subhasish",
};

function greet(city, country) {
  console.log(
    `Hello, I'm ${this.name} from ${city}, ${country}`
  );
}

greet.call(person, "Bengaluru", "India");

greet.apply(person, ["Bengaluru", "India"]);

const greetPerson = greet.bind(person);

greetPerson("Bengaluru", "India");

04 / Interview

Test your understanding