Skip to main content

Episode 6 : Understanding the this Keyword in JavaScript 🧭

Introduction

The this keyword in JavaScript behaves differently depending on where and how it is used. Its value is determined at runtime, based on the calling context --- not where the function is defined.

🌍 1. Global Scope

console.log(this);
// Output β†’ window (in browser)
// Output β†’ {} or global (in Node.js)

In browsers, the global object is the window. In Node.js, the global object is global. So, this in the global context refers to the global object. 🧠 Note: The global object differs across environments.

🧩 2. Function Scope

function x() {
console.log(this);
}
x();
// Output β†’ window (in non-strict mode)
// Output β†’ undefined (in strict mode)

🚨 Strict Mode and this

"use strict";
function x() {
console.log(this);
}
x();
// Output β†’ undefined

In non-strict mode, this defaults to the global object (window). In strict mode, this remains undefined.

πŸ”„ 3. This Substitution Phenomenon JavaScript uses a behavior called "this substitution": When this inside a function is undefined (in non-strict mode), JS automatically substitutes it with the global object. Example:

function x() {
console.log(this);
}
x(); // β†’ window (this substitution)
window.x(); // β†’ window

πŸ’‘ When called as a reference (object.method()), this refers to the object before the dot.

🧱 4. this Inside Objects (Method Call) When a function is a property of an object, it is called a method.

const obj = {
a: 10,
x: function () {
console.log(this);
},
};
obj.x();
// Output β†’ obj

βœ… Inside an object method, this refers to the object itself. ⚠️ However, if you extract the method and call it separately, this will no longer refer to the object.

const func = obj.x;
func();
// Output β†’ window (non-strict) or undefined (strict)

5 call, apply, and bind Methods These are ways to explicitly control the value of this

-🧩 call() Used to call a function with a specific this context and arguments passed individually.

function greet(greeting, name) {
console.log(`${greeting}, ${name}! I'm ${this.role}`);
}
const person = { role: "Developer" };
greet.call(person, "Hello", "Akshay");
// Output β†’ Hello, Akshay! I'm Developer

🧩 apply() Same as call(), but arguments are passed as an array.

greet.apply(person, ["Hi", "John"]);
// Output β†’ Hi, John! I'm Developer

🧩 bind() Returns a new function with this permanently bound to the provided object.

const boundGreet = greet.bind(person, "Hey", "Alice");
boundGreet();
// Output β†’ Hey, Alice! I'm Developer

βš™οΈ Use bind() when you want to store a function with a fixed this for later execution.

⚑ 6. Arrow Functions and this Arrow functions don't have their own this. They lexically inherit it from their enclosing (parent) scope.

const obj = {
a: 42,
x: () => {
console.log(this);
},
};
obj.x();
// Output β†’ window (not obj!)

Explanation: Arrow functions capture this from the surrounding scope at the time of creation. Here, this refers to the global scope, not obj.

βœ… Correct Example with Regular Function

const obj = {
a: 42,
x: function () {
console.log(this);
},
};
obj.x();
// Output β†’ obj

πŸ’‘ Enclosing Lexical Context (in Arrow Functions)

When we say that an arrow function inherits this from its enclosing lexical context, we mean:

Arrow functions don’t have their own this binding.

Instead, they capture (or close over) the this value from the nearest non-arrow (regular) function or execution context in which they are defined.

🧠 7. Summary Table

ContextValue of thisNotes
Global Scopewindow / globalDepends on environment
Function (non-strict)windowthis substitution
Function (strict)undefinedNo substitution
Method in objectThe objectDepends on call site
Arrow FunctionLexical thisInherited from parent
call, apply, bindExplicit thisManually controlled

🎯 Key Takeaways

  • βœ… this depends on how a function is called, not where it’s defined.
  • ⚑ Arrow functions don’t have their own this β€” they lexically inherit it from their parent scope.
  • 🧩 Use call(), apply(), or bind() to manually control the value of this.
  • πŸ”’ Always enable "use strict" to avoid unintended this substitution.

πŸ“Ί Watch Live On YouTube​

Namaste JS Episode 6 Thumbnail