Skip to main content

Command Palette

Search for a command to run...

Understanding this in JavaScript

Updated
4 min readView as Markdown

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

Understanding this helps developers work with:

  • Objects

  • Methods

  • Classes

  • Event handlers

In this guide, you will learn what this represents, how it behaves in different situations, and how calling context changes its value.


What this Represents

The value of this depends on how a function is called.

Key Idea:

this usually refers to the caller of the function.


Simple Example

const user = {
  name: "Akhtar",

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

user.greet();

Output:

Akhtar

Why:

this refers to the user object because the method was called using user.greet().


Caller → Function Relationship

Object Calls Function
        ↓
this points to that object

this in Global Context

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

Browser Example:

console.log(this);

In browsers:

window object

this Inside Objects

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

Example:

const car = {
  brand: "Toyota",

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

car.showBrand();

Output:

Toyota

Different Contexts of this

Global Context
      ↓
this → global object

---------------------

Object Method
      ↓
this → calling object

this Inside Regular Functions

Inside regular standalone functions, this behaves differently.

Example:

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

test();

In browsers (non-strict mode):

window object

In strict mode:

undefined

How Calling Context Changes this

The value of this changes depending on how the function is called.


Example 1: Object Method Call

const person = {
  name: "Akhtar",

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

person.greet();

this Value:

person object

Example 2: Function Stored Separately

const person = {
  name: "Akhtar",

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

const sayHello = person.greet;

sayHello();

Problem:

The original object caller is lost.

Result:

undefined

or global object behavior depending on mode.


Why This Happens

Important:

this is determined during function execution, not where the function is written.


Object Method Example (Simple View)

person.greet()
      ↓
person becomes this

Function Call Example

greet()
   ↓
No object caller
   ↓
Different this behavior

Common Beginner Confusion

Important:

this does not always refer to:

  • The current function

  • The function itself

Instead:

  • It depends on the caller.

Real-World Usage of this

this is heavily used in:

  • Object methods

  • Classes

  • DOM event handlers

  • React class components

  • Constructor functions


Example with Constructor Function

function User(name) {
  this.name = name;
}

const user1 = new User("Akhtar");

console.log(user1.name);

What Happens:

this refers to the newly created object.


Why Understanding this Matters

Understanding this helps developers:

  • Avoid unexpected bugs

  • Understand object behavior

  • Work with classes and methods correctly

  • Build better JavaScript applications


Suggestions (Best Practices)

  • Think of this as the function caller

  • Practice with small object method examples

  • Focus on calling context carefully

  • Avoid memorizing complex rules initially

  • Compare object method calls with normal function calls

  • Use console logging to observe this


Conclusion

  • this represents the current execution context

  • Its value depends on how a function is called

  • Inside object methods, this usually refers to the calling object

  • In regular functions, behavior changes based on execution context

  • Understanding calling context is the key to mastering this

Learning this is an important step toward understanding advanced JavaScript concepts like classes, constructors, and event handling.