Understanding the 'this' Keyword in JavaScript

Learning web development in public. Writing simple, real-world explanations about web development concepts. Helping beginners understand why things work, not just how.
Search for a command to run...

Learning web development in public. Writing simple, real-world explanations about web development concepts. Helping beginners understand why things work, not just how.
No comments yet. Be the first to comment.
Every day, millions of users upload photos, videos, stories, and reels to Instagram. From a user's perspective, the process appears simple: select media, apply filters, add a caption, and tap "Post."
Building Offline-First Messaging Apps: How Messages Work Without Internet Modern messaging applications have transformed the way people communicate. Whether it's chatting with friends, collaborating w
In this article we'll explore about the Expo Router and React Navigation and answer which one to use in 2026. If you build mobile apps using React Native, one thing becomes obvious very quickly: Navig

Modern mobile apps are no longer just a collection of screens connected together. Apps like Instagram, WhatsApp, Uber, and Netflix operate at massive scale with millions of users, real time systems, o
In this article we'll be exploring react.js and the things of react.js that makes it popular and stand out among other libraries ( no fight over library vs framework ). We'll go through: What problem

Shkaai
68 posts
The this keyword is one of the most misunderstood concepts in JavaScript, but the core idea is simple. The value of this depends on how a function is called, not where it is written. It represents the current execution context, meaning the object that is invoking the function at that moment.
this representsthis refers to the object that is currently executing the function. It is determined at runtime, so the same function can have different values of this depending on how it is called.
this in global contextIn the global scope:
console.log(this);
In the browser, this will refer to the window object. In Node.js, it typically refers to an empty object or the global object depending on the environment. The idea is that when there is no specific caller, this defaults to the global context.
this inside objectsWhen a function is called as a method of an object, this refers to that object.
const user = {
name: "Alexa",
greet: function () {
console.log(this.name);
}
};
user.greet();
In this case, this refers to user, so the output is "Alexa". The object before the dot decides what this is.
this inside functionsWhen a normal function is called on its own, it does not have an owner object.
function greet() {
console.log(this);
}
greet();
In the browser, this will refer to the global object. In strict mode, it will be undefined. This happens because the function is not being called by any object. And in node.js we'll get reference to global object, similarly undefined in strict mode.
Arrow functions behave differently because they do not have their own this. Instead, they inherit this from the surrounding scope.
const user = {
name: "Alexa",
greet: () => {
console.log(this.name);
}
};
user.greet();
Here, this does not refer to user. It refers to whatever the surrounding context is, which is usually the global scope, so the result is undefined.
thisThe most important thing to understand is that this changes based on how a function is called.
const user1 = {
name: "Alexa",
greet() {
console.log(this.name);
}
};
const user2 = {
name: "Sam"
};
user2.greet = user1.greet;
user2.greet();
Even though the function was originally defined in user1, when it is called using user2, this refers to user2, so the output is "Sam".
Another example:
const user = {
name: "Alexa",
greet() {
console.log(this.name);
}
};
const fn = user.greet;
fn();
Here, the function is called without an object, so this becomes undefined (or global in non-strict mode), and the result is not what you might expect.
The value of this is not fixed. It is determined by how a function is called. In the global context, it refers to the global object. Inside object methods, it refers to the object calling the method. Inside normal functions, it defaults to global or undefined depending on strict mode. Arrow functions do not have their own this and instead inherit it from their surrounding scope. Once you focus on how a function is invoked, understanding this becomes much clearer.