Understanding the this Keyword in JavaScript

π§ Think of this like:
π βWho is calling me?β
Not who created the function.
Not where itβs written.
π Only: who is calling it right now
π 1. this in Global Context
Example:
console.log(this);
What it means:
In browser β
this=windowIn Node.js β
this={}(module object)
Real-world analogy:
π You are standing alone, no one called you
β‘οΈ So you represent the global environment
π¦ 2. this inside Object (Method)
Example:
const user = {
name: "Ankita",
greet: function () {
console.log(this.name);
}
};
user.greet();
Output:
Ankita
Why?
π user.greet() β user is calling the function
β‘οΈ So this = user
Real-world analogy:
π© Ankita says:
"Hello, my name is Ankita"
π Here speaker = Ankita β so this = Ankita
β οΈ 3. this inside normal function
Example:
function show() {
console.log(this);
}
show();
Output:
Browser β
windowStrict mode β
undefined
Why?
π No object is calling it
β‘οΈ So it falls back to global
Real-world analogy:
π’ Someone shouts in a room:
"Hello!"
π No one knows who said it
β‘οΈ Default = βglobal personβ
π― 4. this inside object BUT function called separately
Example:
const user = {
name: "Ankita",
greet: function () {
console.log(this.name);
}
};
const fn = user.greet;
fn();
Output:
undefined
Why?
π fn() β called WITHOUT object
β‘οΈ So this = global
Real-world analogy:
π© Ankitaβs dialogue recorded:
Then someone else plays it:
"My name is Ankita"
π But speaker is now unknown
β‘οΈ So this is lost
π 5. this depends on CALLER (MOST IMPORTANT)
Example:
function greet() {
console.log(this.name);
}
const user1 = { name: "Ankita", greet };
const user2 = { name: "Rahul", greet };
user1.greet(); // Ankita
user2.greet(); // Rahul
Why?
π Same function
π Different caller
β‘οΈ this changes dynamically
Real-world analogy:
π€ Same mic, different speakers:
Ankita speaks β "I am Ankita"
Rahul speaks β "I am Rahul"
π Mic is same (function)
π Speaker changes (this)
π 6. How Calling Context Changes this
π THIS is the core concept
Case 1: Direct call
greet();
β‘οΈ this = global
Case 2: Object call
user.greet();
β‘οΈ this = user
Case 3: Assigned to variable
const fn = user.greet;
fn();
β‘οΈ this = global
Case 4: Using call()
greet.call({ name: "Ankita" });
β‘οΈ this = { name: "Ankita" }
Real-world analogy:
Think of call() like:
π You force someone to speak:
"Hey, say this as Ankita"
π§© 7. this inside Arrow Function (Important Difference)
Example:
const user = {
name: "Ankita",
greet: () => {
console.log(this.name);
}
};
user.greet();
Output:
undefined
Why?
π Arrow function DOES NOT have its own this
β‘οΈ It takes from parent (global)
Real-world analogy:
πΆ Child repeats parentβs identity:
βI am whoever my parent isβ
π’ Final Real-World Example (BEST FOR FRESHERS)
Scenario: Company Employees
const employee = {
name: "Ankita",
role: "Developer",
introduce: function () {
console.log(`Hi, I am \({this.name}, working as \){this.role}`);
}
};
employee.introduce();
Case 1: Normal
employee.introduce();
β‘οΈ "Hi, I am Ankita..."
Case 2: Borrow function
const emp2 = {
name: "Rahul",
role: "Tester"
};
emp2.introduce = employee.introduce;
emp2.introduce();
β‘οΈ "Hi, I am Rahul..."
Case 3: Detached
const intro = employee.introduce;
intro();
β‘οΈ β Undefined
π₯ Final Rule (Remember This Always)
π this = object before dot (.)
| Code | this |
|---|---|
user.greet() |
user |
fn() |
global |
obj.method() |
obj |
call() |
custom object |
β‘ 1. Nested Object + Function (Classic Confusion)
const user = {
name: "Ankita",
details: {
age: 22,
getAge: function () {
console.log(this.age);
}
}
};
user.details.getAge();
β Output:
22
Why?
π details.getAge() β caller = details
β‘οΈ this = details
β Now tricky version:
const fn = user.details.getAge;
fn();
β Output:
undefined
Why?
π Called without object
β‘οΈ this = global
π§ Real-world:
Inside company β you are employee
Outside β identity lost
β‘ 2. this lost inside callback (VERY IMPORTANT)
const user = {
name: "Ankita",
greet: function () {
setTimeout(function () {
console.log(this.name);
}, 1000);
}
};
user.greet();
β Output:
undefined
Why?
π setTimeout calls function
β‘οΈ NOT user
β‘οΈ So this = global
β Fix using arrow function:
const user = {
name: "Ankita",
greet: function () {
setTimeout(() => {
console.log(this.name);
}, 1000);
}
};
user.greet();
β Output:
Ankita
π§ Real-world:
You tell your assistant:
"Remind them my name"
Normal function β assistant forgets who you are
Arrow β assistant remembers you
β‘ 3. Method Borrowing (Same function, different this)
function introduce() {
console.log(`I am ${this.name}`);
}
const user1 = { name: "Ankita" };
const user2 = { name: "Rahul" };
user1.intro = introduce;
user2.intro = introduce;
user1.intro();
user2.intro();
β Output:
I am Ankita
I am Rahul
π§ Key Idea:
π Function same
π Caller changes β this changes
β‘ 4. Chain Call (Important Interview Pattern)
const obj = {
name: "Ankita",
greet() {
console.log(this.name);
return this;
}
};
obj.greet().greet();
β Output:
Ankita
Ankita
Why?
π greet() returns this
β‘οΈ So chaining keeps same object
π§ Real-world:
Like:
"I speak β I continue speaking β still me"
β‘ 5. this inside class (Real Project Usage)
class User {
constructor(name) {
this.name = name;
}
greet() {
console.log(this.name);
}
}
const u1 = new User("Ankita");
u1.greet();
β Output:
Ankita
β Problem:
const fn = u1.greet;
fn();
β Output:
undefined
β Fix:
const fn = u1.greet.bind(u1);
fn();
π§ Real-world:
You take employee out of company
β‘οΈ They forget role
β‘οΈ bind = attach identity permanently
β‘ 6. this in Event Listener (VERY REAL WORLD)
<button id="btn">Click</button>
<script>
document.getElementById("btn").addEventListener("click", function () {
console.log(this);
});
</script>
β Output:
π Button element
Why?
π Button is calling handler
β‘οΈ this = button
β Arrow version:
document.getElementById("btn").addEventListener("click", () => {
console.log(this);
});
β Output:
π window
π§ Real-world:
Normal function β button speaks
Arrow β outer world speaks
β‘ 7. Object inside Function inside Object (Deep Confusion)
const user = {
name: "Ankita",
greet: function () {
function inner() {
console.log(this.name);
}
inner();
}
};
user.greet();
β Output:
undefined
Why?
π inner() is normal function
β‘οΈ Not called by user
β‘οΈ this = global
β Fix:
const user = {
name: "Ankita",
greet: function () {
const inner = () => {
console.log(this.name);
};
inner();
}
};
β‘ 8. Trick Question (Interview Favorite)
const obj = {
name: "Ankita",
greet: function () {
console.log(this.name);
}
};
(obj.greet)();
β Output:
Ankita
BUT:
(0, obj.greet)();
β Output:
undefined
Why?
π (0, obj.greet) removes object reference
β‘οΈ becomes normal function call
π₯ FINAL MASTER RULE
π Ask ONLY ONE question:
β Who is calling the function?
That is your this.
π§ Quick Cheat Sheet
| Situation | this |
|---|---|
obj.method() |
obj |
fn() |
global |
setTimeout(fn) |
global |
arrow fn |
parent this |
event handler |
element |
bind() |
fixed |
β One-Line Summary
π this is decided at runtime, based on who calls the function β not where it's written.




