Skip to main content

Command Palette

Search for a command to run...

Understanding the this Keyword in JavaScript

How Calling Context Determines this

Updated
6 min readView as Markdown
Understanding the this Keyword in JavaScript

1. What this Represents

The simplest way to understand this is: it points to whoever is calling the function. Not where the function is written, not where it sits in the code—only the caller matters.

If you look at a function call like ironMan.speak(), then this inside speak will be ironMan. That’s it. That’s the rule you keep coming back to.

People usually mess this up because they assume this belongs to the object where the function is defined. It doesn’t. It’s decided at runtime.

const ironMan = { 
    name: "Tony Stark", 
    speak() { 
        console.log(this.name); 
    } 
};

ironMan.speak();

2. this in Global Context

When you call a function without any object, there is no clear caller, In that case Javascript refers to default.

In the browser, this becomes the global object. Global object here refers to window So if we have something like a global hero = "Hulk" and the function that log this.hero , and when you call that function it will print "Hulk" .

As this is not a feature it is a fallback. So in strict mode, javascript even removes this behavior and sets this to undefined to avoid bugs.

3. this inside Objects

In JavaScript, the this keyword used inside an object method refers to the object that is calling the method. This allows methods to access and use the object's properties and other methods.

const thor = { 
    name: "Thor", 
    speak() { 
    console.log(this.name); 
    } 
};

Now in this example when you call thor.speak() output will be "Thor" because thor is the caller.

const fn = thor.speak; 
fn();

Now it prints undefined (or global value). Why? Because the caller is no longer thor. You removed the context.

This is the exact moment where most people realize that this is not tied to the object—it’s tied to the call.

4. this inside function

In javascript, the value of this keyword in a function is depend on how the function is called also known as the execution context, not depend on where it is defined.

4.1 Standalone Function call

It means when a function is called without any object connection like myfunction() in this function this refer to a global object window in browser. Now it has also two types in non-strict mode this -> window in browser. and in "strict mode" this -> undefined

function showThis() { 
    console.log(this); 
 }
 showThis(); 
// Output: Window (or global) in non-strict mode; 
// undefined in strict mode

4.2 Object Method Call

When a function is called as a method inside an Object for example myobject.mymethod() in this case this will refers to object that contains the method at the time of the call.

const student = {
    name: "Piyush",
    greet: myfunction() {
        console.log("Namaste" + this.name); // this will refer to the object -> student                 
    }
};
student.greet();

// Output: Namaste Piyush

4.3 Arrow Function

Arrow functions don’t behave like normal functions when it comes to this. They don’t create their own this at all. Instead, they just take this from wherever they were created.
Think of it like this:
A normal function decides this when it is called.
An arrow function locks this when it is defined.

const avengers = {
    team: "Avengers"
    members: ["Iron Man", "Thor"],
    
    showMember() {
        function inner () {
            console.log(this.team);
        }
        inner (_;
    }
};
avengers.showMember();

// Output: undefined 

showMember() is called by avengers → so inside it, this = avengers
But inner() is called as a normal function
No object is calling inner()
So, this inside inner → global / undefined

const avenger = {
    team: "Avengers",
    
    showMember() {
    const inner = () => {
        console.log(this.team);
    };
    inner();
    }
};
avenger.showMember();

// Output: Avengers

The arrow function didn’t create a new this. Instead, it captured the this from showTeam().
And inside showTeam() -> this = avengers
So inside inner -> this is still avengers

5. How calling context changes this in javascript

this in JavaScript is not about where a function is written — it’s about how it is called. That’s the part people get wrong. We’ll break this down step by step, because if you don’t internalize call-site, you’ll keep guessing this instead of knowing it.

5.1 Default Binding

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

show();

In non-strict modethis = window (browser)
In strict modethis = undefined
Because there is no object to the left of the call, so JS falls back to default binding.

5.2 Implicit Binding (Method Call)

const user = {
     name: "Piyush", 
     greet() { 
        console.log(this.name); 
    } 
};
user.greet();

Here: this = user
Because the function is called with an object: user.greet()
The object before the dot becomes this.

5.3 Explicit Binding (call, apply, bind)

function greet() { 
    console.log(this.name); 
}
const user = { name: "Piyush" };
greet.call(user);

Now: this = user

Difference:

call → executes immediately
apply → same but arguments as array
bind → returns a new function

6. Conclusion

At the end of the day, this in JavaScript isn’t some mysterious keyword—it’s simply a reference to who is calling the function.

If you keep trying to link this to where the function is written, you’ll keep getting confused. JavaScript doesn’t care about where a function lives. It only cares about how that function is invoked at runtime.

In the global context, this points to the global object. Inside an object, it points to that object—but only if the method is called through it. Inside a regular function, it can fall back to undefined (or window in non-strict mode). And the moment you change the calling context—like assigning a method to a variable or passing it around—you also change what this points to.
That’s the key shift:
this is not fixed. It’s dynamic. It follows the call-site.

So instead of memorizing rules, train yourself to ask one question every time:

“Who is calling this function right now?”