Skip to main content

Command Palette

Search for a command to run...

Mastering JavaScript 'this': A Guide to Call, Apply, and Bind

Learn how to control function context and borrow methods like a pro.

Updated
3 min readView as Markdown

In JavaScript, the keyword this is one of the most powerful yet confusing concepts for beginners. It doesn’t have a fixed value; instead, its value depends entirely on how and where a function is called. Think of this as a reference to the "owner" of the current execution.

1. What this Means (The Simple Version)

Imagine the word "this" in a conversation. If I say, "Look at this car," the meaning changes depending on which car I am pointing at.

In JavaScript, this acts like a pointer. It points to an object, but which object it points to is determined by the "call site" (where the function is executed).

2. this in Different Contexts

Inside an Object

When a function is part of an object (called a method), this refers to the object itself.

const user = {
    name: "Janhvi",
    greet: function() {
        console.log("Hi, I am " + this.name);
    }
};

user.greet(); // Hi, I am Janhvi

In this case, this = user.

Inside a Normal Function

When you use this inside a standard function that isn't part of an object, it usually points to the Global Object (in a browser, that’s the window object).

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

showThis(); // Logs the Window object

3. Taking Control: call(), apply(), and bind()

Sometimes, you want to tell a function exactly what this should point to, regardless of how it was called. We use these three methods to "borrow" functions or force a specific context.

call()

The call() method calls a function and allows you to pass in the object you want to use as this as the first argument. Any additional arguments are passed one by one.

const student = { name: "Aryan" };

function introduce(major) {
    console.log(this.name + " studies " + major);
}

introduce.call(student, "Data Science"); // Aryan studies Data Science

apply()

apply() is almost identical to call(). The only difference is how you pass arguments. Instead of listing them one by one, you pass them as an array.

introduce.apply(student, ["Computer Science"]); // Aryan studies Computer Science

bind()

Unlike the other two, bind() does not call the function immediately. Instead, it returns a new function with the this value permanently locked to the object you provided.

const welcomeAryan = introduce.bind(student, "AI");
welcomeAryan(); // Aryan studies AI

4. Comparison Table: Call vs. Apply vs. Bind

Method

Execution

Argument Format

Return Value

call()

Immediate

Comma-separated (arg1, arg2)

Result of function

apply()

Immediate

Array ([arg1, arg2])

Result of function

bind()

Delayed

Comma-separated

A new function

5. Why Do We Need These?

These methods are essential for Function Borrowing. If you have a complex method in one object, you can use call() or apply() to run that method on a completely different object without rewriting the code. This keeps your code "DRY" (Don't Repeat Yourself).

Quirky Behavior: The "Losing this" Problem

A very common bug happens when you try to use an object's method as a callback (like in setTimeout).

const user = {
    name: "Janhvi",
    sayHi: function() { console.log(this.name); }
};

setTimeout(user.sayHi, 1000); // Result: undefined (or empty)

The Quirk: When setTimeout runs the function, it doesn't run it "on" the user object. It just runs the function by itself, so this defaults back to the window. To fix this, you would use .bind(): setTimeout(user.sayHi.bind(user), 1000);.