Skip to main content

Command Palette

Search for a command to run...

Understanding the this Keyword in JavaScript

Updated
3 min read
Understanding the this Keyword in JavaScript

1. What this Represents

In plain English, this refers to an object. Which object? The one that is currently executing the function. It allows you to reuse functions across different objects, making your code more dynamic.
It does NOT depend on where the function is written
It depends on how the function is called

2. this in the Global Context

When you use this outside of any function or object (in the global scope), it refers to the global object.

  • In a Browser: this refers to the window object.

  • In Node.js: this refers to the global object.

console.log(this); // window ,In a browser, this logs the Window object
console.log(this); // {}     ,In a Node.js, this logs the object

3. this Inside Objects (Method Context)

When a function is defined as a property of an object, we call it a method. When you call that method, this refers to the object itself. This is the most common and intuitive use of this.

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

user.greet(); // Output: "Hello, my name is Chai"
// Here, 'user' is the caller, so 'this' = user.

4. this Inside Functions

The behavior changes depending on how the function is invoked:

  • Simple Function Call: If you call a standalone function, this defaults to the global object (window).

  • Strict Mode: If 'use strict'; is enabled, this will be undefined in a regular function call to prevent accidental global changes.

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

show(); 
//  In non-strict mode → window
//  In strict mode → undefined

5. How Calling Context Changes this

The most important takeaway is that this is dynamic. The same function can have a different this depending on who "touches" it.

Consider this example where a function is shared:

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

const person1 = { name: "Alice", identify: identify };
const person2 = { name: "Bob", identify: identify };

person1.identify(); // "Alice" (person1 is the caller)
person2.identify(); // "Bob"   (person2 is the caller)

Summary Table: Different Contexts of this

Context

What this refers to

Global Scope

Global Object (window or global)

Object Method

The Object before the dot (the caller)

Simple Function

Global Object (or undefined in strict mode)

Arrow Functions

The this of the surrounding code (lexical)

JavaScript form basic concepts to Advance

Part 12 of 17

Master JavaScript from the ground up. This series takes you on a complete journey from fundamental syntax and variables to advanced asynchronous patterns, closures, and performance optimization. Whether you are a beginner writing your first line of code or an intermediate developer looking to bridge the gap to seniority, these guides provide deep dives, practical examples, and modern best practices for the 2026 ecosystem.

Up next

Map and Set in JavaScript

1. What is a Map? A Map is a collection of key-value pairs, just like objects — but more powerful. However, the primary difference is that Map allows keys of any type—including functions, objects, and