Skip to main content

Command Palette

Search for a command to run...

Understanding the "this" Keyword in JavaScript

Updated
5 min readView as Markdown

JavaScript has many concepts that confuse beginners, and one of the biggest is the this keyword. At first glance, it looks simple, but its value changes depending on how a function is called.

If you understand one core idea, everything becomes easier:

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

This blog explains how this works in different situations with simple examples and practical explanations.


What Does this Represent?

The this keyword is a special reference created automatically by JavaScript.

It points to an object related to the current function execution.

The important part is this:

  • this is not decided where the function is written

  • this is decided by how the function is called

That is why the same function can produce different outputs depending on the caller.

Example:

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

person.greet();

Output:

Aslam

Here:

  • greet() is called by person

  • So this becomes person

  • this.name becomes "Aslam"


this in Global Context

In the global scope of a browser, this refers to the global object.

In browsers, the global object is window.

Example:

console.log(this);

Output in browser:

Window {...}

You can even verify this:

console.log(this === window);

Output:

true

This means global variables in browsers become properties of window.

Example:

var message = "Hello";

console.log(window.message);

Output:

Hello

this Inside Objects

One of the most common uses of this is inside object methods.

A method is simply a function stored inside an object.

Example:

const car = {
  brand: "Toyota",

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

car.showBrand();

Output:

Toyota

Why?

Because showBrand() is called using car.

So:

this === car

This is the easiest way to understand this.


Simple Mental Model

Think of this as:

"Who called this function?"

If:

user.login();

Then inside login():

this === user

The caller matters more than where the function was created.


this Inside Regular Functions

Inside a normal standalone function, this behaves differently.

Example:

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

show();

In browsers, output is usually:

Window {...}

Because the function is called globally.

But this often creates confusion because beginners expect this to refer to the function itself.

It does not.

Functions do not automatically become this.

The caller decides it.


How Calling Context Changes this

This is the most important section.

The exact same function can produce different values of this.

Example:

function introduce() {
  console.log(this.name);
}

const user1 = {
  name: "Aslam",
  intro: introduce
};

const user2 = {
  name: "Rahul",
  intro: introduce
};

user1.intro();
user2.intro();

Output:

Aslam
Rahul

Notice something important:

  • The function is the same

  • Only the caller changes

When called by user1:

this === user1

When called by user2:

this === user2

This proves that calling context controls this.


Losing the Original Context

A common mistake happens when a method is stored separately.

Example:

const user = {
  name: "Aslam",

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

const anotherFunction = user.greet;

anotherFunction();

Output in browser:

undefined

Why?

Because now the function is no longer called by user.

Instead:

anotherFunction();

is a normal function call.

So this becomes the global object instead of user.

Since the global object does not have name, the result becomes undefined.

This is one of the most common JavaScript mistakes.


this in Arrow Functions

Arrow functions behave differently.

They do not create their own this.

Instead, they take this from their surrounding scope.

Example:

const user = {
  name: "Aslam",

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

user.greet();

Output:

undefined

Why?

Because arrow functions do not use the object as this.

They inherit this from where they were created.

This is why regular functions are usually better for object methods.

Correct version:

const user = {
  name: "Aslam",

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

user.greet();

Output:

Aslam

Key Rules to Remember

1. this depends on the caller

object.method();

Here:

this === object

2. Normal function calls use global context

myFunction();

In browsers:

this === window

3. Arrow functions do not create their own this

They inherit it from outside.


4. The same function can have different this values

Because different objects can call it.


Common Beginner Mistake

Many beginners think:

"this belongs to the function"

That is incorrect.

this belongs to the execution context created during the function call.

The caller controls it.

Once you understand this rule, most confusion disappears.


Final Thoughts

The this keyword becomes much easier when you stop thinking about where the function is written and start thinking about how it is called.

The core rule is simple:

The object before the dot usually becomes this.

Examples:

user.login()
car.start()
person.walk()

In each case:

  • user becomes this

  • car becomes this

  • person becomes this

Mastering this is important because it appears everywhere in JavaScript:

  • Objects

  • Event handlers

  • Classes

  • DOM manipulation

  • Frameworks like React and Vue

Once you understand calling context, JavaScript code becomes far more predictable and easier to debug.

1 views