Skip to main content

Command Palette

Search for a command to run...

Understanding this in JavaScript

Updated
4 min read

One concept that often confuses JavaScript beginners is the this keyword. Unlike many other programming languages, the value of this in JavaScript depends on how a function is called, not where it is defined.

Understanding this is important because it is frequently used when working with objects, methods, and event handlers.

In this article, we will cover:

  • What this represents

  • this in the global context

  • this inside objects

  • this inside functions

  • How the calling context changes the value of this


What Does this Represent?

In simple terms, this refers to the object that is calling the function.

You can think of it as:

this → the caller of the function

The value of this is determined at the time the function is executed, not when it is written.


this in the Global Context

When this is used in the global scope, it refers to the global object.

Example in a browser environment:

console.log(this);

In browsers, the output is typically:

window

This means the global object (window) is the value of this.


this Inside Objects

When a function is called as a method of an object, this refers to that object.

Example:

const user = {
  name: "Dipan",
  greet: function () {
    console.log("Hello, my name is " + this.name);
  }
};

user.greet();

Output:

Hello, my name is Dipan

Explanation:

  • The function greet is called using user.greet()

  • Therefore, this refers to user


this Inside Regular Functions

When a function is called normally (not as part of an object), this behaves differently.

Example:

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

showThis();

In browsers (non-strict mode), the output is usually:

window

This happens because the function is called in the global context.


How Calling Context Changes this

The most important rule about this is:

The value of `this` depends on how the function is called.

Consider this example:

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

person.sayName();

Output:

Dipan

Here, this refers to person.


Now consider this variation:

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

const anotherFunction = person.sayName;

anotherFunction();

Output:

undefined

Explanation:

  • The function is no longer called as person.sayName()

  • It is called independently

  • Therefore, this no longer refers to person

This demonstrates that the caller determines the value of this.


Simple Rule to Remember

A useful way to think about this is:

Look at the object before the dot when the function is called.

Example:

object.method()

In this case:

this → object

Why Understanding this Is Important

The this keyword appears frequently in JavaScript when working with:

  • Object methods

  • Event handlers

  • Constructor functions

  • Classes

Understanding how this works helps avoid unexpected behavior and makes object-oriented JavaScript easier to work with.


Conclusion

The this keyword in JavaScript refers to the object that calls a function, and its value depends on how the function is executed.

Key points to remember:

  • this represents the caller of a function

  • In the global context, it refers to the global object

  • Inside object methods, this refers to the object itself

  • Inside regular functions, this often refers to the global object

  • The calling context determines the value of this

By understanding how this changes based on the calling context, you can write clearer and more predictable JavaScript code.