JS Tutorial



JS THIS KEYWORD


JavaScript this Keyword 👤

The this keyword in JavaScript refers to the object that is executing the current function.

🔍 Global Scope

In the global scope, this refers to the global object (window in browsers):

console.log(this); // window (in browser)

📦 Inside a Function

function showThis() {
  console.log(this);
}
showThis(); // window (in non-strict mode)

🏠 Inside an Object Method

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

🧭 Arrow Functions Don't Bind this

Arrow functions inherit this from their lexical scope (parent):

const user = {
  name: "Sam",
  greet: () => {
    console.log("Hi, I am " + this.name);
  }
};
user.greet(); // Hi, I am undefined

🎮 Interactive Example

Click to see how this works inside an object method:

📘 Summary

  • this in global → global object (like window)
  • this in function (non-strict) → global object
  • this in object method → that object
  • this in arrow function → parent scope
Tip: Use traditional functions when you want your method to use its own this, and arrow functions when you want it to inherit from its parent.

🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review