30 Days of Code: Arrow Functions in JavaScript

🚀 30 Days — 30 Coding Mistakes Beginners Make Day 14/30 I called an object method… and it printed: Hello undefined 😐 const user = { name: "Sam", greet: () => console.log(this.name) } The mistake: Arrow functions don’t have their own `this`. They inherit `this` from the outer scope, so it was not pointing to the object. Fix 👇 Use a normal function for object methods. Arrow functions are great for callbacks, but not for object methods. Small syntax change. Correct behavior. Day 15 tomorrow 👀 #30DaysOfCode #javascript #reactjs #frontend #webdevelopment #codeinuse

  • No alternative text description for this image

const user = { name: "Sam", greet: () => { console.log("Hello", user.name); } }; user.greet(); Output: Hello Sam Arrow functions → avoid for object methods

To view or add a comment, sign in

Explore content categories