JavaScript 'this' Keyword Explained with Example

Most developers use JavaScript every day… but few of us actually know what really happens under the hood when our code runs. As a curiosity, I decided to dive a little deeper into JS and share my learning journey with others like me. 🚀 🤔 Quick question: In JavaScript, what does `this` actually refer to? When I started learning JS, I thought `this` always pointed to the function itself.   Turns out… it’s all about **how you call the function** 👇 const car = {  brand: "Tesla",  start() {   console.log(this.brand);  } }; car.start(); // "Tesla" const startCar = car.start; startCar();  // undefined 💡 Why the difference? - In `car.start()`, `this` → the `car` object. - In `startCar()`, `this` → global scope (so `brand` is undefined). ### Takeaway The value of `this` in JavaScript depends on the **calling context**, not where the function is written. 👉 What was your biggest “aha moment” with JavaScript? #JavaScript #WebDevelopment #FullStack #LearningInPublic

To view or add a comment, sign in

Explore content categories