Skip to main content

Command Palette

Search for a command to run...

Understanding the this Keyword in JavaScript

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

Chapter: JavaScript mein this ko Samajhna (Hinglish Guide)

JavaScript mein this ek aisa concept hai jo beginners ko thoda confusing lagta hai. Reason simple hai — this ka value fixed nahi hota. Yeh depend karta hai ki function kaun call kar raha hai.

Chalo isko simple parts mein samajhte hain.

1. this kya represent karta hai?

Simple language mein:

👉 this ka matlab hota hai “abhi function ko kaun call kar raha hai”

Na ki:

  • function kahan likha hai

  • ya function ka naam kya hai

2. Global Context mein this

Jab this kisi function ya object ke bahar use hota hai, to woh global object ko point karta hai.

Browser mein:

console.log(this);

➡️ Output: window

Node.js mein:

➡️ Output: {} (module scope mein)

👉 Simple rule:
Global level pe this = global environment

3. Object ke andar this

Jab this object ke method ke andar use hota hai, to woh us object ko refer karta hai

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

user.greet();

Output:

Rahul

👉 Kyun?
Kyuki user ne greet() call kiya hai
➡️ isliye this = user

👉 Rule:
object.method() → this = object

4. Normal Function ke andar this

Agar function normal tareeke se call hota hai, to this ka behavior change ho jata hai.

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

show();

Output:

  • Strict mode: undefined

  • Normal mode: window (browser)

👉 Important point:
Standalone function ka koi owner nahi hota, isliye context lost ho jata hai

5. Calling Context ka effect on this

Yeh sabse important part hai.

Case 1: Object method call

const obj = {
  value: 10,
  show() {
    console.log(this.value);
  }
};

obj.show();

this = obj

Case 2: Function ko alag variable mein store karna

const fn = obj.show;
fn();

Ab this obj nahi raha
context lost ho gaya

Case 3: Nested function

const obj = {
  value: 10,
  show() {
    function inner() {
      console.log(this.value);
    }
    inner();
  }
};

obj.show();

inner() ka this alag ho jata hai
object se link break ho jata hai

6. Golden Rule (Sabse important)

👉 this = function ka caller

Bas yahi yaad rakho.

7. Easy Tarike se Samajhne ki Tips

Socho:

“Is function ko call kaun kar raha hai?”

Mat socho:

“Function kahan likha hai”

Context

this Ki Value

Asli Matlab (Simple Hindi)

Global Scope

window (Browser) / global (Node)

Jab aap kisi function ke bahar hote ho, toh poori duniya (Global) hi aapka ghar hai.

Object Method

The Object (e.g., user)

Agar function ko kisi object ne dot (.) laga kar call kiya hai, toh wahi object this hai.

Simple Function Call

undefined (Strict) / window

Agar function bina kisi owner ke "akele" call hua hai, toh woh apna rasta bhatak jata hai.

Nested Function

window / undefined

Ek function ke andar dusra function ho, toh andar wala function apne parent ka context kho deta hai (Lost).

More from this blog