Understanding the this Keyword in JavaScript
One of the most confusing concepts in JavaScript is the this keyword. Many beginners struggle with it because its value is not fixed—it changes depending on how a function is called.
But don’t worry. Once you understand one simple idea, everything becomes clear:
👉 this refers to the object that is calling the function (the caller).
In this blog, we’ll break it down step by step in a simple and intuitive way.
🔹 What Does this Represent?
The this keyword refers to the current execution context, or simply:
👉 The object that is calling the function.
🔍 Diagram Idea: Caller → Function Relationship
Object (caller) → calls → Function
↓
this = Object
✅ Example
const user = {
name: "Kanishka",
greet: function() {
console.log(this.name);
}
};
user.greet(); // Kanishka
👉 Here, user is calling the function → so this = user.
🔹 this in Global Context
The value of this in the global scope depends on the environment.
✅ In Browser
console.log(this);
👉 Output:
Window object
💡 Explanation
In browsers:
Global object =
windowSo
thisrefers towindow
⚠️ In Strict Mode
"use strict";
console.log(this);
👉 Output:
undefined
🔹 this Inside Objects
When a function is called as a method of an object:
👉 this refers to that object.
✅ Example
const car = {
brand: "BMW",
showBrand: function() {
console.log(this.brand);
}
};
car.showBrand(); // BMW
🔍 Diagram Idea: Object Context
car → showBrand()
this = car
🔹 this Inside Functions
This is where things get tricky.
❌ Regular Function (Non-Strict Mode)
function test() {
console.log(this);
}
test();
👉 Output:
Window object
❌ Regular Function (Strict Mode)
"use strict";
function test() {
console.log(this);
}
test();
👉 Output:
undefined
💡 Why?
Because the function is not called by any object, so:
- No caller →
thisbecomes global or undefined
🔹 this in Arrow Functions
Arrow functions behave differently.
👉 They do NOT have their own this.
Instead, they inherit this from their surrounding (parent) scope.
✅ Example
const user = {
name: "Kanishka",
greet: function() {
const arrowFunc = () => {
console.log(this.name);
};
arrowFunc();
}
};
user.greet(); // Kanishka
👉 Arrow function takes this from greet().
🔹 How Calling Context Changes this
This is the most important concept.
👉 this depends on HOW the function is called, not where it is written.
✅ Example 1
const person = {
name: "Kanishka",
greet: function() {
console.log(this.name);
}
};
person.greet(); // Kanishka
❌ Example 2
const greetFunc = person.greet;
greetFunc(); // undefined or error
👉 Now:
Function is called independently
No object →
thisis lost
🔍 Diagram Idea: Context Change
person.greet() → this = person
greetFunc() → this = undefined/window
🔹 Controlling this Manually
JavaScript provides methods to control this:
call()apply()bind()
✅ Example with call()
const user1 = { name: "Kanishka" };
const user2 = { name: "Yash" };
function greet() {
console.log(this.name);
}
greet.call(user1); // Kanishka
greet.call(user2); // Yash
💡 What Happens?
👉 We manually set this to different objects.
🔹 Real-World Examples
✅ 1. Object Methods
const student = {
name: "Kanishka",
print: function() {
console.log(this.name);
}
};
✅ 2. Event Handlers
button.addEventListener("click", function() {
console.log(this); // button element
});
👉 this refers to the element that triggered the event.
✅ 3. Classes
class User {
constructor(name) {
this.name = name;
}
greet() {
console.log(this.name);
}
}
const u = new User("Kanishka");
u.greet();
🔹 Common Mistakes to Avoid
❌ Losing this
const obj = {
name: "Kanishka",
greet: function() {
setTimeout(function() {
console.log(this.name); // undefined
}, 1000);
}
};
✅ Fix with Arrow Function
setTimeout(() => {
console.log(this.name);
}, 1000);
❌ Using Arrow Function as Method
const obj = {
name: "Kanishka",
greet: () => {
console.log(this.name); // undefined
}
};
👉 Arrow functions should not be used as object methods.
🔹 Quick Summary of this
| Context | Value of this |
|---|---|
| Global (browser) | window |
| Object method | Object itself |
| Regular function | window / undefined |
| Arrow function | Inherited from parent |
| Event handler | Element triggering event |
🔹 When to Use Arrow vs Regular Functions
Use Regular Functions When:
Working with object methods
You need dynamic
this
Use Arrow Functions When:
You want to preserve
thisInside callbacks
🔹 Final Thoughts
The this keyword might seem tricky at first, but it becomes easy when you remember:
👉 Focus on HOW the function is called, not where it is defined.
🔹 Golden Rule
👉 this = the object that calls the function