JavaScript Output Challenge: Guess the Result

💥Can You Guess the Output(Advanced JavaScript Edition) 🚀 Output Challenge for JavaScript Developers! Let’s test how deep your JS fundamentals really go 👇 What will be the output of this code? (No AI, no console — just brain + logic 🧠) const obj = { name: 'Rohit', greet() { console.log(`Hello, ${this.name}`); }, delayedGreet() { setTimeout(this.greet, 1000); }, }; obj.delayedGreet(); 🧩 Think before you scroll away — Most developers get this wrong in live interviews (even senior ones). 👉 Drop your answer below in the comments Explain why it happens, not just what happens. Let’s see how many get it right 🔥 #JavaScript #Frontend #WebDevelopment #React #Nextjs #CleanCode #DeveloperCommunity #MachineCodingRound #InterviewPreparation

"This" keyword basically depends on the way functions(lets not talk about arrow functions😭) are called. Here setTimeout basically takes the callback and store it somewhere and call that callback after timer runs out. When the call happens, its called at global(global execution context) level. And since there we dont have name, it prints 'Hello Undefined'. To fix this issue, we can bind greet function with this of delayedGreet which has name pointing to 'rohit'. Something like below. delayedGreet() {  setTimeout(this.greet.bind(this), 1000);  },

To view or add a comment, sign in

Explore content categories