Skip to main content

Command Palette

Search for a command to run...

Understanding this in JavaScript

Updated
•4 min read•View as Markdown

Let’s start with a question that confuses almost every beginner:

What does this actually mean in JavaScript?

You might expect a simple answer like:

“this refers to the current object”

But that’s only sometimes true.

The real idea is simpler and more powerful:


What this Really Represents

👉 this refers to who is calling the function.

Not where the function is written. Not where it is defined.

👉 It depends on how the function is called.


Start with a Simple Example

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

user.greet();

Output:

Alice

Why?

Because:

  • greet() is called by user

  • So this = user


this in Global Context

When you use this outside any function:

console.log(this);

In browser:

👉 this = window

In Node.js:

👉 this = {} (module object)


Mental Model

Global scope → this refers to global object

this Inside Objects

This is the most common and easiest case.

const car = {
  brand: "Tesla",
  showBrand: function () {
    console.log(this.brand);
  }
};

car.showBrand();

Output:

Tesla

Why?

Because:

  • car.showBrand() → called by car

  • So this = car


this Inside Functions

Now things get interesting.

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

test();

In browser:

👉 this = window

In strict mode:

👉 this = undefined


Important Point

When a function is called alone:

functionCall()

👉 There is no owner → this becomes global (or undefined in strict mode)


How Calling Context Changes this

This is the most important concept.

Let’s use the same function in different ways.


Example 1: Function inside object

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

person.greet();

👉 this = person


Example 2: Store function separately

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

const greetFn = person.greet;

greetFn();

👉 Output:

undefined

Why?

Because now:

greetFn()

👉 No object is calling it 👉 this is no longer person


Key Insight

this is decided at CALL TIME, not definition time

Simple Real-World Analogy

Think of this like:

📞 Who is speaking?

Same sentence:

“My name is John”

Depends on who says it.

  • If John says it → correct

  • If Alice says it → different meaning

👉 this works the same way.


Common Beginner Confusions


❌ “this always refers to object”

Not true. Only when called via object.


❌ “this depends on where function is written”

Wrong. It depends on how it is called.


Quick Summary Table

Situation Value of this
Global scope window / global
Object method the object
Regular function global / undefined
Detached function loses original this

Visual Mental Model 🧠

object.method()  → this = object
function()       → this = global/undefined

Small Practice Exercise

Try this in your console.


1️⃣ Predict output

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

user.sayName();

2️⃣ Now detach function

const fn = user.sayName;
fn();

👉 Why is output different?


3️⃣ Modify object

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

Try calling it in different ways.


Final Thoughts

this feels confusing at first because it changes based on context.

But once you remember:

👉 “Who is calling the function?”

Everything becomes much clearer.


Diagram Ideas

1️⃣ Object call vs function call 2️⃣ this binding flow 3️⃣ Call-site based decision diagram


If you want next:

  • this in arrow functions (very important)

  • bind, call, apply

  • real interview questions on this

Just tell me 🚀