Skip to main content

Command Palette

Search for a command to run...

Understanding the this Keyword in JavaScript

"this" keyword in JavaScript

Published
β€’4 min read
Understanding the this Keyword in JavaScript
P

My name is 𝐏𝐫𝐚𝐀𝐚𝐬𝐑 and I talk about 𝗧𝗲𝗰𝗡-𝐊𝐧𝐨𝐰π₯𝐞𝐝𝐠𝐞, π—ͺπ—²π—―π——π—²π˜ƒ, π——π—²π˜ƒπ—’π—½π˜€ and π—Ÿπ—Άπ—³π—²π˜€π˜π˜†π—Ήπ—².

Introduction

The this keyword is one of the most confusing concepts in JavaScript for beginners. The reason is simple: its value is not fixed. It changes depending on how and where a function is called.

Instead of thinking of this as something complicated, you should understand one simple rule:

this refers to the object that is calling the function.

Once you understand the caller, you understand this.

What is this

In JavaScript, this is a special keyword that refers to the execution context of a function. In simpler terms, it points to the object that is currently calling the function.

Think of it like this:

Caller β†’ Function β†’ this

Whoever calls the function becomes the value of this.

this in Global Context

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

In Browser

console.log(this); 

Output:

window

In Node.js

console.log(this);

Output:

{}

The value depends on the environment, but the idea remains the same: global context means this points to the global object.

this Inside Objects

When a function is inside an object and is called using that object, this refers to the object.

Example

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

user.greet();

Output:

Prakash

Here, user is calling greet(), so this refers to user.

Visual Understanding

Image Image Image Image Image Image

Object β†’ Method β†’ this = object

this Inside Functions

This is where most confusion happens.

Example

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

show();

In non-strict mode (browser), this will point to the global object.

In strict mode:

"use strict";

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

show();

Output:

undefined

So inside normal functions, this depends on how the function is called, not where it is written.

How Calling Context Changes this

This is the most important concept.

The value of this depends on the call site.

Case 1: Direct Function Call

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

test();

this β†’ global object (or undefined in strict mode)

Case 2: Object Method Call

const obj = {
  name: "JavaScript",
  show() {
    console.log(this.name);
  }
};

obj.show();

this β†’ obj

Case 3: Assigning Method to Variable

const obj = {
  name: "JavaScript",
  show() {
    console.log(this.name);
  }
};

const fn = obj.show;
fn();

Output:

undefined

Here, the caller is no longer obj. It becomes a normal function call.

Caller-Based Model

Different calls β†’ Different this

  • obj.method() β†’ this = obj

  • function() β†’ this = global/undefined

  • detached function β†’ this lost

Before vs After Understanding

Confusing Approach

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

const greetFn = user.greet;
greetFn(); // undefined

Correct Thinking

Do not look at where the function is defined. Look at how it is called.

Common Mistakes

Mistake 1: Thinking this is fixed

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

const fn = obj.show;
console.log(fn()); // undefined

Mistake 2: Losing Context

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

setTimeout(user.greet, 1000); // undefined

Because the function is passed as a callback, it loses its original caller.

Simple Rule to Remember

If you remember only one thing, remember this:

this = the object that calls the function

If there is no object, this becomes global or undefined.

Benefits of Understanding this

Understanding this helps you:

  • Write correct object-oriented code

  • Avoid bugs in callbacks and event handlers

  • Work effectively with frameworks like React and Node.js

  • Debug unexpected undefined values

Conclusion

The this keyword is not complicated once you stop thinking about where the function is written and start focusing on how it is called.

Every time you see this, ask one question: Who is calling this function?

That answer will always give you the correct value of this.


❊ Want More…?

I write articles on blog.prakashtsx.com and also post development-related content on the following platforms: