Skip to main content

Command Palette

Search for a command to run...

Understanding the this Keyword in JavaScript Who's Calling?

Updated
4 min read
Understanding the this Keyword in JavaScript  Who's Calling?
M

Hello, I’m Asim, a passionate Full Stack Developer with 1 year of hands-on experience in building scalable and user-friendly web applications. I work across both frontend and backend, enjoy solving real-world problems, and focus on writing clean, efficient, and maintainable code. I’m always eager to learn new technologies and grow as a developer.

If you've written Javascript for more than a week , you've probably run into this and wondered why it does'nt always point to what you expected. Don't worry. By the end of this Blog "this" will complete make sense.

lets break it down from scratch.


What is "this" ?

In Javascript this is a special keyword that refers to the object that is currently calling the function. think of it like a name tag thats gets assigned at the moment a function is called not when its defined.

Simple rule this = whoever called the function.

That's the core idea. Everything else is just variations of this rule applied in different contexts.


this in Global Context .

When you use this outside of any function or object at the top level of your script it refers to the global object.

  • In a browser, the global object is window.

  • In Node.js, it's global.

console.log(this); 

At the global level, no one specific is "calling" anything, so JavaScript defaults to the global object as the caller.

var name = "JavaScript";
console.log(this.name); 

Note This only works with var, not let or const, because let/const don't attach to the global object.


this Inside Objects .

When a function is defined inside an object and called as a method, this refers to that object because the object is the one doing the calling.

const user = {
  name: "Asim",
  greet: function () {
    console.log("Hello, I am " + this.name);
  }
};

user.greet(); // Hello, I am Asim

Here, user is calling greet(). So inside greet, this points to user. Simple!

Now look at what happens if we pull the method out:

const greetFn = user.greet;
greetFn(); // Hello, I am undefined

The function is the same but now greetFn is being called by the global scope, not user. So this changes. This is where most beginners get confused.

The function didn't change. The caller did**.**


this Inside Regular Functions .

Inside a regular function (not a method), this depends on how the function is called.

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

showThis(); 

When you call a function without an explicit object before it, the global object becomes the implicit caller.

In strict mode ("use strict"), however, this inside a regular function becomes undefined instead of the global object. This is actually safer behavior.

"use strict";

function showThis() {
  console.log(this); // undefined
}

showThis();

How Calling Context Changes this .

This is the heart of it. this is not fixed at definition time it's decided at call time.

Let's see three ways the same function behaves differently depending on who calls it

function introduce() {
  console.log("My name is " + this.name);
}

const personA = { name: "Sara", introduce };
const personB = { name: "Zara", introduce };

personA.introduce(); // My name is Sara
personB.introduce(); // My name is Zara
introduce();         // My name is undefined (global caller)    

The function introduce is identical in all three cases. What changes is who is calling it.


Arrow Functions and this .

Arrow functions are special they don't have their own this. Instead, they inherit this from the surrounding (lexical) scope.

const team = {
  name: "Dev Squad",
  members: ["Ali", "Bilal", "Sana"],
  showMembers: function () {
    this.members.forEach((member) => {
      console.log(member + " is in " + this.name);
    });
  }
};

team.showMembers();
// Ali is in Dev Squad
// Bilal is in Dev Squad
// Sana is in Dev Squad

If we had used a regular function inside forEach, this would have been the global object. The arrow function borrows this from showMembers, where this is team.


Conclusion .

this is one of those JavaScript concepts that feels tricky at first but once you internalize the golden rule, it becomes second nature.

To recap:

  • this is not about where a function is written it's about who calls it.

  • In the global scope, this is the global object.

  • Inside an object method, this is that object.

  • In a regular function called alone, this is global (or undefined in strict mode).

  • Arrow functions don't have their own this they borrow it from their parent scope.

Next time this confuses you, just pause and ask "Who is calling this function right now?" the answer will always point you in the right direction.

Happy coding!