The call() method is a built-in JavaScript function method that allows you to call a function with a specific this context and individual arguments.

1.Syntax

functionName.call(thisArg, arg1, arg2, ...);
  • functionName → the function you want to invoke
  • thisArg → the value of this inside the function
  • arg1, arg2… → optional arguments passed individually

2.Basic Example

function greet(greeting) {
console.log(`${greeting}, my name is ${this.name}`);
}
const user = { name: "Zaman" }; 
greet.call(user, "Hello"); // Hello, my name is Zaman

this inside greet is now user object
✔ Argument "Hello" is passed individually

3.Using call() With Multiple Arguments

function introduce(age, city) {
console.log(`${this.name} is ${age} years old from ${city}`);
}
const user = { name: "Ahmed" };

introduce.call(user, 30, "Dhaka");
// Ahmed is 30 years old from Dhaka

4.Borrowing Methods From Other Objects

const person1 = {
fullName() {
return `${this.firstName} ${this.lastName}`;
}
};
const person2 = { firstName: "Zaman", lastName: "Sarder" };

console.log(person1.fullName.call(person2)); // Zaman Sarder

✔ Useful to reuse methods across objects

Categorized in:

Javascript,