Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is .call in JavaScript

Introduction to .call in JavaScript

Understanding .call in JavaScript is like learning how to direct a movie. You're the director, your functions are the actors, and .call is the script you give them. It tells them what role to play, and what context to play it in. In this blog post, we're going to pull back the curtain and discover how .call allows us to control the context in which a function is executed.

The Basics of .call

In JavaScript, functions are like the actors in our movie. They perform actions, and they can do so in various contexts. To change the context in which a function operates, or in other words, to change the "this" value of a function, we use the .call method.

Let's look at an example:

function introduce(name) {
    console.log(`Hello, my name is ${this.name}`);
}

let person = {name: "Alice"};

introduce.call(person, "Alice");

In this script, we've defined a function called 'introduce' that logs a greeting to the console. We've also created a 'person' object with a 'name' property. When we call 'introduce.call(person, "Alice")', JavaScript changes the context of 'introduce' to the 'person' object, and the name "Alice" is passed as an argument to the function.

Understanding "this"

Before we go any further, let's take a step back and talk about the "this" keyword in JavaScript. In our movie analogy, "this" is the character that the actor is playing. When you're inside a function, "this" refers to the object that function is a method of. But if a function isn't a method of an object, then "this" refers to the global object (which is 'window' in a browser, and 'global' in node.js).

Directing Functions with .call

So how does .call change the "this" value of a function? When you call a function using .call, the first argument you pass is the new "this" value, and any subsequent arguments are passed to the function.

function greet(greeting) {
    console.log(`${greeting}, I'm ${this.name}`);
}

let person = {name: "Bob"};

greet.call(person, "Good morning");

Here, we're using .call to change the context of the 'greet' function to the 'person' object. The argument "Good morning" is passed to the 'greet' function, and is used for the greeting in the console.log statement.

Using .call with Built-in Functions

One of the powerful features of .call is that it can be used with built-in JavaScript functions. This allows you to borrow methods from other objects and use them in a different context.

let fruits = ['apple', 'banana', 'cherry'];

let capsFruits = Array.prototype.map.call(fruits, fruit => fruit.toUpperCase());

console.log(capsFruits); 

In this example, we're using .call to borrow the Array.prototype.map function and use it on the 'fruits' array. The result is a new array, 'capsFruits', where each fruit name has been converted to uppercase.

When to Use .call

Like a good movie director, knowing when to use .call is as important as knowing how to use it. .call is especially useful when you need to use a method that belongs to another object, or when you need to manipulate the "this" value of a function for a specific purpose.

Conclusion

Learning .call in JavaScript is like unlocking a new level in a video game. It opens up new possibilities, gives you greater control over your functions, and helps you write more flexible and powerful code. It's a bit like having a magical remote control for your functions. You can point it at any function, press a button, and voila! You've changed the context in which that function operates. So next time you're coding in JavaScript, remember: .call is your friend. Use it wisely, and you'll be directing your functions like a pro.