Skip to main content

Command Palette

Search for a command to run...

Understanding this Keyword in JavaScript

Updated
6 min readView as Markdown

One of the most confusing topics for JavaScript beginners is:

this

At first, it may seem mysterious because its value changes depending on where and how it is used.

But the core idea is actually simple.

In most situations:

this refers to the object that is calling the function.

Understanding this is extremely important because it is used everywhere in JavaScript:

  • Objects

  • Classes

  • Event handlers

  • Frameworks

  • DOM interactions

In this beginner-friendly guide, you’ll learn:

  • What this represents

  • this in global context

  • this inside objects

  • this inside functions

  • How calling context changes this

The goal is to build intuition, not memorize complicated rules.


What Does this Represent?

this is a special keyword in JavaScript.

It refers to:

The current execution context or caller of a function.


Simple Way to Think About this

Ask this question:

"Who is calling this function?"

The answer usually becomes the value of this.


Real-World Analogy

Imagine different people using the same remote control.

The action performed depends on:

  • Which TV the remote is currently controlling

Similarly:

  • this depends on who is calling the function.

this Depends on Context

Unlike normal variables, this changes based on:

  • Where function is used

  • How function is called


Different Contexts of this

Global Context
Object Method
Regular Function
Class Method
Event Handler

We’ll focus on beginner-friendly cases.


this in Global Context

When this is used outside any object or function, it refers to the global object.


Example in Browser

console.log(this);

In browsers, this usually refers to:

window

Why?

Because browser global scope is attached to the window object.


Global Context Diagram

Global Scope
     │
     ▼
this → window

this Inside Objects

This is the most important beginner use case.


Example

const person = {

  name: "Hitesh",

  greet: function () {

    console.log(this.name);

  }

};

person.greet();

Output:

Hitesh

Understanding What Happened

Who called the function?

person.greet()

The caller is:

person

So:

this = person

Object Method Visualization

person object
     │
     ▼
greet() called
     │
     ▼
this → person

Why this.name Works

Because:

this

points to the object:

person

So:

this.name

becomes:

person.name

Another Example

const car = {

  brand: "Toyota",

  showBrand: function () {

    console.log(this.brand);

  }

};

car.showBrand();

Output:

Toyota

Again:

this → car

this Inside Regular Functions

Now let’s look at normal standalone functions.


Example

function test() {

  console.log(this);

}

test();

In Browser

Inside a regular function in browsers:

this → window

Why This Confuses Beginners

Because:

  • Inside objects → this becomes object

  • Inside normal functions → this becomes global object

The key idea remains:

this depends on how function is called.


Caller → Function Relationship

Object calls function
       │
       ▼
this = object


Regular function call
       │
       ▼
this = global object

How Calling Context Changes this

This is the MOST important concept.

The value of this changes depending on the caller.


Example

const user1 = {
  name: "Rahul",

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

const user2 = {
  name: "Priya"
};

user2.greet = user1.greet;

user2.greet();

Output:

Priya

Why Did This Happen?

Even though function originally belonged to user1:

It was called using:

user2.greet()

So:

this → user2

Important Rule

this is determined by:

HOW function is called

—not where function was created.


Common Beginner Analogy

Think of this like a microphone.

Whoever is holding the microphone becomes the speaker.

Similarly:

Whoever calls the function becomes this.


Common Beginner Mistakes

1. Forgetting this

Wrong:

const person = {

  name: "Hitesh",

  greet: function () {

    console.log(name);

  }

};

This causes error because name is not directly defined.


Correct Version

console.log(this.name);

2. Confusing Function Context

Beginners often expect:

this

to always refer to the same object.

But it changes dynamically.


Simple Rule to Remember

Ask:

"Who called the function?"

That usually solves the confusion.


Real-World Usage of this

The this keyword is heavily used in:

  • Classes

  • Objects

  • Event handlers

  • Frameworks

  • DOM interactions

Libraries and frameworks like React and Vue.js frequently use context-related concepts internally.


Visual Summary of this

GLOBAL CONTEXT
this → window


OBJECT METHOD
this → object


REGULAR FUNCTION
this → global object
(browser)

Simple Comparison Table

Situation Value of this
Global scope Global object
Object method Calling object
Regular function Global object (browser)

Assignment Practice

Try this yourself.


Task

  1. Create an object called student

  2. Add:

    • name

    • age

  3. Add method:

    • showDetails()
  4. Use this inside method

  5. Print student details


Example Solution

const student = {

  name: "Rahul",

  age: 20,

  showDetails: function () {

    console.log(this.name);

    console.log(this.age);

  }

};

student.showDetails();

Output

Rahul
20

Don’t Try to Memorize Everything

The this keyword feels confusing initially for almost every JavaScript developer.

That’s normal.

Focus on the main idea:

this usually refers to the caller of the function

With practice, understanding this becomes much easier.


Final Thoughts

The this keyword is one of the most important concepts in JavaScript.

You learned:

✅ What this represents ✅ this in global context ✅ this inside objects ✅ this inside functions ✅ How calling context changes this

The biggest idea to remember is:

this is determined by how a function is called.

Once this concept becomes clear, many advanced JavaScript concepts start making much more sense.

2 views