Skip to main content

Understanding the this Keyword in JavaScript

Updated 6 min readMarkdown

The this keyword is one of the most confusing parts of JavaScript for beginners.

Most people try to memorize rules like:

  • “Inside objects, this means object”

  • “Inside functions, this means window”

  • “Arrow functions behave differently”

Then one day JavaScript changes the behavior and the brain immediately blue-screens.

The real way to understand this is much simpler.

this depends on how a function is called.

Not where it is written.

Not where it is created.

The caller decides the value of this.

That single idea explains most of JavaScript’s behavior.


What Does this Represent?

this refers to the object that is currently calling the function.

Think of it like:

Who called the function?

That caller usually becomes this.

Example:

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

user.greet();

Output:

Gagan

Why?

Because:

user called greet()

So inside greet(), this becomes user.


Mental Model of this

Instead of memorizing rules, think like this:

caller → function

Whoever is left of the dot usually becomes this.

Example:

person.sayHello();

Here:

person = caller

So:

this === person

this in Global Context

In the global scope, this usually refers to the global object.

In browsers:

console.log(this);

Output:

window

Because the browser global object is window.

So:

this === window

in the browser global scope.


Diagram: Global Context

Global Scope
     ↓
this → window

this Inside Objects

Inside object methods, this refers to the object calling the method.

Example:

const car = {
  brand: "BMW",

  showBrand() {
    console.log(this.brand);
  }
};

car.showBrand();

Output:

BMW

Because:

car called showBrand()

So:

this === car

Diagram: Object Method Context

car
 ↓ calls
showBrand()
 ↓
this = car

this Inside Regular Functions

Now things get weird.

Example:

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

test();

In browsers (non-strict mode):

window

Why?

Because no object called the function.

The browser automatically falls back to the global object.

So:

this === window

Important Reality Check

Beginners often think:

“Functions belong to objects.”

No.

Functions are independent.

The caller determines this.

That is why the same function can behave differently depending on how it is called.


How Calling Context Changes this

This is the most important section.

Look at this:

const user = {
  name: "Gagan",

  greet() {
    console.log(this.name);
  }
};

user.greet();

Output:

Gagan

Now:

const fn = user.greet;

fn();

Output in browser:

undefined

Why?

Because:

fn() has no object caller

So this is no longer user.

The calling context changed.

The function stayed the same.

This is where beginners realize JavaScript operates like a raccoon holding loose electrical wires.


Same Function, Different this

const person1 = {
  name: "Aman",
  greet() {
    console.log(this.name);
  }
};

const person2 = {
  name: "Riya"
};

person2.greet = person1.greet;

person2.greet();

Output:

Riya

Why?

Because now:

person2 called greet()

So:

this === person2

Again:

The caller decides this.


Diagram: Caller Relationship

person2
   ↓ calls
 greet()
   ↓
this = person2

Common Beginner Mistake

People think:

this

means:

the object where the function is written

Wrong.

It depends on:

how the function is called

That distinction changes everything.


Why this Feels Confusing

Because JavaScript allows:

  • functions to move around

  • methods to be reassigned

  • functions to be passed as callbacks

Which means:

calling context changes constantly

And therefore:

this changes constantly

Simple Rule to Remember

Whenever you see this, ask:

Who called this function?

That question solves most confusion.


Real-World Example

Imagine this:

const youtube = {
  channel: "CodeMaster",

  playVideo() {
    console.log(this.channel);
  }
};

When:

youtube.playVideo();

Output:

CodeMaster

Because YouTube object called the method.

Now:

const play = youtube.playVideo;

play();

Now there is no caller object.

So this changes.


Strict Mode Note

In strict mode:

"use strict";

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

test();

Output:

undefined

Instead of window.

This was introduced to avoid accidental global behavior.


Arrow Function Mention (High-Level Only)

Arrow functions behave differently.

They do not create their own this.

Instead, they inherit this from the surrounding scope.

Example:

const user = {
  name: "Gagan",

  greet: () => {
    console.log(this.name);
  }
};

This usually does not behave the way beginners expect.

That is why arrow functions are generally avoided as object methods.

For now, just remember:

Regular functions → own this
Arrow functions → inherited this

Practical Mental Model

Global Scope

this → global object

Object Method

object.method()
↓
this = object

Regular Function

functionCall()
↓
No explicit caller
↓
window or undefined

Final Mental Shortcut

Do not memorize random rules.

Just inspect:

Who called the function?

That caller usually becomes this.

This single idea explains most JavaScript this behavior without turning your brain into tangled charging cables.


Conclusion

The this keyword in JavaScript represents the calling context of a function.

Its value depends on how the function is called.

Key takeaways:

  • In global scope, this usually refers to the global object

  • Inside object methods, this refers to the calling object

  • Inside regular functions, this depends on the calling context

  • Changing the caller changes this

  • Arrow functions inherit this from surrounding scope

Understanding this becomes much easier once you stop memorizing rules and start thinking in terms of function callers and calling context.

1 view

No comments yet. Be the first to comment.