Understanding JavaScript 'this' keyword with bind() method

🧠 Most JavaScript devs misunderstand this 👀 Especially those with 1–2 years of experience. No frameworks. No libraries. Just core JavaScript fundamentals. 🧩 Output-Based Question (this & bind) const user = { name: "Alex", getName() { return this.name; } }; const fn = user.getName; const boundFn = fn.bind(user); console.log(fn()); console.log(boundFn()); ❓ What will be printed? (Don’t run the code ❌) A. Alex Alex B. undefined Alex C. Alex undefined D. Throws an error 👇 Drop your answer in the comments Why this matters This question tests: how this works in JavaScript method vs function invocation implicit vs explicit binding why bind() exists When fundamentals aren’t clear: this feels unpredictable bugs appear “random” debugging gets painful Good developers don’t guess. They understand execution context. 💡 I’ll pin the explanation after a few answers. #JavaScript #WebDevelopment #CodingFundamentals #JavaScriptTips #DevCommunity #Programming #TechEducation #SoftwareDevelopment #JavaScriptForBeginners #UnderstandingThis

  • text, letter

the answer will be "B" because...when you do the user.getname()...the function just pass the referene.. so this === undefined.so tbe full function will be undefined. in second case, when you called boundFn then the this === user. So, it this.name = alex.. So, it will print alex.

Ah yes, the weird and confusing javascript that we all like to "program" in.

Answer: B Since assigning an object's method to a variable makes it lose its 'this' context so fn returns undefined, whereas Function.bind() sets the given argument as the function's 'this' context again, returning 'Alex'.

Basically if we run this code exactly most probably it will throw the Syntax Error because it has some extra commas (,) After correcting syntax error it should be B

Like
Reply
See more comments

To view or add a comment, sign in

Explore content categories