Understanding the this Keyword in JavaScript

Hi LinkedIn 👋 Continuing my journey in Advanced JavaScript, I explored one of the most confusing yet powerful concepts — the this keyword. this behaves differently compared to other keywords because its value depends on how a function is called, not where it is defined. Here are my key learnings: 🔹 Calling this globally console.log(this); // window object 🔹 Inside a normal function (ES5) function test() { console.log(this); } test(); // window (undefined in strict mode) 🔹 Inside an arrow function (ES6) const test = () => { console.log(this); } Arrow functions don’t have their own this — they inherit it from the surrounding scope. 🔹 Inside an object method let obj = { name: "Amit", value: function () { console.log(this); } } Here, this refers to the object itself. 🔹 Inside event listeners btn.addEventListener("click", function () { console.log(this); // button element }); 🔹 Using bind const boundFn = test.bind(obj); boundFn(); // this = obj 📌 Key takeaway: Understanding this is all about understanding execution context. Still exploring more real-world use cases to strengthen this concept 🚀 #JavaScript #FrontendDevelopment #WebDevelopment #LearningJourney

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories