Understanding the “this” Keyword in JavaScript as a Beginner!

Mohit Singh
Software Engineer
Are you learning JavaScript and finding the this keyword a little tricky? You’re not alone! The this keyword can be confusing at first, but don’t worry—I’m here to explain it in the simplest way possible. By the end of this post, you’ll understand how this works, and you’ll be ready to get started with it!
In this beginner-friendly guide, we’ll break down the this keyword in JavaScript with easy examples and explanations. Let’s dive in!
What is "this" in JavaScript?
Imagine you’re at a birthday party, and everyone has a name tag on their shirt. When you want to talk about yourself, you point to your name tag and say, “This is me!” In JavaScript, this works the same way—it helps code know which object it’s talking about.
But here’s the twist: the meaning of this can change based on how and where you use it in your code. Let’s explore some examples to see how this behaves in different scenarios.

1. Using “this” Inside an Object Method
Let’s start by looking at an object with a simple method. Think of this object as a toy box:
const toyBox = {
color: "red",
showColor: function() {
console.log(this.color);
}
};
toyBox.showColor(); // Output: "red"
Here, we have an object called toyBox with a property color and a method showColor. When we call toyBox.showColor(), the this keyword refers to the object it’s inside—toyBox. So, when the function runs, this.color means the color property of the toyBox, and it prints “red.”
Note it down: When you use this inside an object’s method, it refers to the object the method belongs to.
2. “this” in a Standalone Function
Now, what happens if we use this inside a regular function that isn’t part of an object?
function sayName() {
console.log(this.name);
}
sayName(); // Output: undefined(in strict mode) or refers to the global object (in non-strict mode)
In this example, we define a standalone function sayName(). When we call it, this doesn’t have an object to refer to, so JavaScript defaults to the global object. In browsers, this is typically the window object, which doesn’t have a name property, so it prints undefined.
If you’re in strict mode, JavaScript won’t default to the global object, and this will be undefined.
Takeaway: In a regular function, this often refers to the global object (or undefined in strict mode).
3. “this” in Arrow Functions
Arrow functions behave differently from regular functions when it comes to this. Arrow functions don’t have their own this; instead, they inherit this from where they were defined.
Let’s say we have an object with an arrow function:
const person = {
name: "Sam",
sayHello: () => {
console.log(this.name);
}
};
person.sayHello(); // Output: undefined
Here, we use an arrow function inside the person object. But since arrow functions don’t have their own this, they inherit it from the surrounding context. In this case, the surrounding context is the global scope, which doesn’t have a name property. So, this.name is undefined.
Point to note: Arrow functions don’t have their own this; they inherit it from the surrounding code where they were created.
Why is “this” Important in JavaScript?
Understanding how this works is crucial for writing flexible and efficient JavaScript code. By knowing how this changes depending on context, you can avoid common bugs and make your code easier to read and maintain. Whether you’re working with objects, callbacks, or event handlers, knowing how to handle this will help you write cleaner code.
Conclusion
The this keyword in JavaScript can seem a bit mysterious at first, but once you break it down, it’s not so tricky after all! Remember, this changes depending on the context: in object methods, it refers to the object itself; in standalone functions, it often points to the global object; and in arrow functions, it inherits this from the surrounding context.
Keep practicing with different examples, and soon enough, you’ll be using this like a JavaScript expert!🚀