JavaScript Interview Trap: Lexical this and Arrow Functions

🚨 Even Experienced Developers FAIL Interviews Because of This JS Trap (3–5+ years of experience… and still pause here 👀) No frameworks. No libraries. No async tricks. Just pure JavaScript behavior. 🧠 Output-Based Question (this + Arrow Functions) const user = { name: "Kaushal", greet: () => { console.log(this.name); } }; user.greet(); ❓ What will be printed? ❌ Don’t run the code 🧠 Think like the JavaScript engine A. "Kaushal" B. undefined C. "" (empty string) D. Throws an error 👇 Drop ONE option only (no explanations yet 👀) ⚠️ Why This Breaks Interviews Most developers assume: • Arrow functions behave like normal object methods • this depends on who calls the function • Defining a method inside an object auto-binds context All three assumptions fail here. And interviewers know it. 🎯 What This Actually Tests • Lexical this • Arrow vs regular function behavior • Invocation context rules • Why React callbacks sometimes log undefined When your mental model of this is wrong: • Event handlers break • Object methods lose context • Production bugs appear silently This isn’t a syntax trap. It’s a context trap. Strong JavaScript developers don’t guess. They understand how this is bound. 💡 I’ll pin the full breakdown after a few answers. #JavaScript #JSFundamentals #CodingInterview #FrontendDeveloper #FullStackDeveloper #DevelopersOfLinkedIn #SoftwareEngineering #JSInterviewSeries

  • graphical user interface, text, application

Answer : B , because this keyword don't have context of name , to log name we have to use bind method

else u can use arrow function

See more comments

To view or add a comment, sign in

Explore content categories