Understanding the this Keyword in JavaScript
What is this in JavaScript? When to use this keyword?

If you have been learning JavaScript, you have probably seen this keyword and wondered what exactly it refers to. Unlike many other programming concepts, this does not have a fixed value. It's meaning changes depending on how and where a function is called.
The easiest way to understand this is:
this represents the object that is calling the function.
In this blog, we will break down the concept step by step using simple language and real-life examples.
What this Represents
In JavaScript, this is a special keyword that refers to the execution context of a function.
Instead of thinking about complicated internal mechanisms, just remember:
Who is calling the function? That is what this refers to.
RealLife Analogy
Imagine a person saying:
"I am working"
Now the meaning of "I" depends on who is speaking.
If Nikhil says it then "I" = Nikhil
If Rahul says it then "I" = Rahul
Similarly in JavaScript:
this= the object that is speaking (calling the function)
this in Global Context
When this is used outside of any function or object, it refers to the global object.
Example
console.log(this);
In Browser
thisrefers to thewindowobject
Simple Understanding
Think of it as:
"There is no specific caller, so JavaScript assumes the global environment."
this Inside Objects
When a function is inside an object and is called using that object, this refers to the object.
Example
const user = {
name: "Nikhil",
greet: function () {
console.log("Hello, my name is " + this.name);
}
};
user.greet();
Output
Hello, my name is Nikhil
Explanation
The function
greetis called usinguserSo
thisrefers touserTherefore
this.namebecomes"Nikhil"
RealLife Example
Think of a remote control:
TV remote : controls TV
AC remote : controls AC
The same button behaves differently depending on which device is using it.
Similarly:
Same function
Different caller
Different
this
this Inside Regular Functions
When this is used inside a normal function (not inside an object), it behaves differently.
Example
function show() {
console.log(this);
}
show();
In Browser
thisrefers towindow
Why?
Because the function is called without any object:
show(); // no caller object
So JavaScript defaults to the global object.
this Depends on How Function is Called
This is the most important concept.
Example 1
const person1 = {
name: "Nikhil",
greet: function () {
console.log(this.name);
}
};
const person2 = {
name: "Rahul"
};
person2.greet = person1.greet;
person2.greet();
Output
Rahul
Explanation
The function originally belonged to
person1But now it is called by
person2So
thisrefers toperson2
Diagram Idea: Caller → Function Relationship
Same function, different caller, different this.
this in Arrow Functions (Important Note)
Arrow functions do not have their own this. They take this from their surrounding context.
Example
const user = {
name: "Nikhil",
greet: () => {
console.log(this.name);
}
};
user.greet();
Output
undefined
Why?
Arrow functions do not bind their own
thisThey use
thisfrom the outer scope (global here)
Summary
thisrefers to the caller of the functionIn global scope →
thisis the global objectInside object method →
thisis the objectInside normal function →
thisis global (in non-strict mode)Function call determines
this, not where it is writtenArrow functions do not have their own
this
Practical you must try
Whenever you see this, ask:
"Who is calling this function?"
That answer will give you the value of this.
Understanding this becomes easy when you stop memorizing rules and start observing how functions are called.
Practice by changing the caller of the same function and see how this changes. That is the best way to master it.




