Hello! Welcome to our next lesson.
In our last session, we introduced methods and got our first glimpse of the this keyword. You learned that when you call a method like user.introduce(), this inside the method refers to the user object. This is its most common use case, but it's only part of the story.
Today, we'll dive deep into one of JavaScript's most powerful and frequently misunderstood concepts. We'll focus on the learning outcome: Explain the purpose of the 'this' keyword and how its value is determined in a method call. You'll learn that the value of this is dynamic and depends entirely on how a function is invoked. Mastering this is crucial for writing predictable code and is a concept that will reappear constantly as you move towards TypeScript and Next.js.
The Golden Rule of this
The most important thing to understand about this is that its value is determined by the call-site — the way a function is called, not where it is defined.
To start, let's hear this rule explained and see the most common case in action.
The video 'JavaScript this Keyword' from Programming with Mosh provides a very clear and simple rule of thumb for understanding this. We'll watch the first two minutes to establish our foundation.
Watch from the beginning to 02:03. Focus on the core rule: if a function is called as a method on an object, this references that object.
As Mosh explained, the simplest case is method invocation. When a function is called as a property of an object, this is set to the object the method is called on.
const video = {
title: 'My Awesome Video',
play() {
// Because play() was called on 'video',
// 'this' refers to the 'video' object.
console.log(this.title);
}
};
video.play(); // Logs: "My Awesome Video"
A Deeper Look: this and the Prototype Chain
You have a preference for understanding the "why" behind concepts, so let's explore why this works. When you call video.play(), JavaScript needs to find the play method and execute it. The keyword this acts as a reference to the object that served as the starting point for that process.
The following reading provides an excellent explanation of this mechanism.
A Deeper Look at Objects in JavaScript
The article 'A Deeper Look at Objects in JavaScript' explains how this relates to the object that acts as the entry point for a method call. This will give you a stronger mental model.
Please read the section titled 'The this Keyword'. Pay close attention to the explanation of how the lookup for properties starts from whatever this is pointing to.
In short, this refers to the object that our method is bound to at the moment of execution.
Where it Gets Tricky: Function Invocation
What happens if we call the same function, but not as a method of an object? This is where the dynamic nature of this becomes very apparent.
Consider this code:
let user = {
name: 'Alice',
sayHi: function() {
console.log(`Hi, I'm ${this.name}`);
}
};
user.sayHi(); // Logs "Hi, I'm Alice". 'this' is 'user'.
// Now, let's assign the method to a new variable.
let greeting = user.sayHi;
// What happens when we call it now?
greeting(); // Logs "Hi, I'm undefined" (in strict mode).
When we call greeting(), it's just a "standalone" function call. It's no longer attached to the user object at the call-site. In this context, JavaScript sets this to the global object (window in browsers). However, modern JavaScript modules run in strict mode, where this in a standalone function call is undefined to prevent common bugs. Since undefined has no name property, we get an error or undefined.
The following image provides a great visual summary of these two main scenarios.

Test your understanding! (Challenge #1)
What will the following code log to the console? This tests the "method invocation" rule.
const call = {
caller: "mom",
says: function() {
console.log(`Hey, ${this.caller} just called.`);
}
};
call.says();
(A) Hey, undefined just called.
(B) Hey, mom just called.
(C) Hey, caller just called.
Show answer
The correct answer is (B) Hey, mom just called.
Explanation: The function says is invoked as a method of the call object (call.says()). Therefore, inside says, the this keyword refers to the call object, and this.caller correctly resolves to "mom".
(This challenge is based on Challenge #1 from the dev.to article "Take 'this' Quiz, Understand How 'this' Works in JavaScript".)
Test your understanding! (Challenge #2)
Now, what about this scenario? This tests the "standalone function invocation" rule.
const call = {
caller: "mom",
says: function() {
console.log(`Hey, ${this.caller} just called.`);
}
};
let newCall = call.says;
newCall();
(A) Hey, undefined just called.
(B) Hey, mom just called.
Show answer
The correct answer is (A) Hey, undefined just called.
Explanation: We assign the says method to a new variable, newCall. When newCall() is invoked, it's a simple, standalone function call, not a method call on the call object. In this global context (and in strict mode), this is undefined. Therefore, this.caller is undefined.caller, which results in an error or the value undefined. The key is that the invocation newCall() is what determines the value of this.
(This challenge is based on Challenge #3 from the dev.to article.)
The Exception to the Rule: Arrow Functions
Arrow functions (=>) behave differently. They do not have their own this context. Instead, they "inherit" this from their parent scope at the time they are defined. This is called lexical scoping.
This makes them predictable, but it also means they are generally not suitable for object methods if you need to access the object's properties.
What is THIS in JavaScript? in 100 seconds
This fast-paced video from Fireship explains the unique behavior of this in arrow functions. It's a critical concept for modern JavaScript.
Watch from 03:03 to 03:55. Notice the direct comparison between a regular function and an arrow function used as an object method.
Let's look at an example:
const person = {
name: 'Zoe',
// Regular function method
greet: function() {
console.log(`Hi from ${this.name}`); // 'this' is the 'person' object
// Arrow function inside the method
const innerGreeting = () => {
// It inherits 'this' from its parent scope (the greet method)
console.log(`Inner greeting from ${this.name}`); // 'this' is also the 'person' object
};
innerGreeting();
},
// Arrow function as a method
farewell: () => {
// It inherits 'this' from the global scope where 'person' was defined
console.log(`Goodbye from ${this.name}`); // 'this' is undefined in strict mode
}
};
person.greet(); // Logs "Hi from Zoe" and "Inner greeting from Zoe"
person.farewell(); // Logs "Goodbye from undefined"
Test your understanding! (Challenge #3)
Given what you just learned, what will this code log?
const call = {
caller: "mom",
says: () => {
console.log(`Hey, ${this.caller} just called.`);
}
};
call.says();
(A) Hey, undefined just called.
(B) Hey, mom just called.
(C) Hey, caller just called.
Show answer
The correct answer is (A) Hey, undefined just called.
Explanation: The says property is an arrow function. Arrow functions do not get their own this. They inherit this from the surrounding (lexical) context. In this case, the call object is defined in the global scope. Therefore, this inside the arrow function refers to the global this (which is undefined in strict mode), not the call object. The global context has no caller property, so this.caller is undefined.
(This challenge is based on Challenge #2 from the dev.to article.)
Other Important Contexts
Two other scenarios are important for a complete picture of this.
-
Constructor Functions: When you use the
newkeyword to create an object from a function,thisinside that function refers to the brand new object being created. -
Explicit Binding: JavaScript provides methods (
.call(),.apply(),.bind()) to manually set the value ofthisfor a function, overriding all other rules.
This final video gives a quick but comprehensive tour of all the ways this can be determined, including these two.
Let's watch a quick summary that covers all the cases, including this in regular functions, constructor functions, and how to set it explicitly.
Watch from 02:03 to 07:12. This covers this in regular functions, constructors, and a common problem with callbacks, which introduces the idea of explicitly setting the context.
Conclusion
You've just tackled one of the most confusing parts of JavaScript! Understanding how this is determined is a huge step forward and will prevent countless hard-to-find bugs in your future code.
Key Takeaways:
- The value of
thisis determined by how a function is called, not where it's defined. - Method Call (
object.method()):thisis theobject. - Standalone Function Call (
func()):thisis the global object, orundefinedin strict mode. - Arrow Functions (
=>): Have nothis. They inherit it from their parent's scope (lexicalthis). - Constructor Call (
new Func()):thisis the newly created empty object. - You can explicitly set
thisusing.bind(),.call(), or.apply().
Next Up
Now that you have a firm grasp of object methods and the this keyword, our next step is to learn how to work with all the properties in an object at once. In the next lesson, we will explore the learning outcome: Iterate over an object's properties using the for...in loop and Object.keys(). This will allow you to dynamically access and process object data.