Lesson 5 of 80
Why this depends on how a function is called, not where it's written — the exact opposite of variable scope.
15 min read
Every option explains itself — why the right one is right, and why each wrong one is wrong. Miss one and it comes back later on a spaced-repetition schedule.
Question 1 of 2
What determines the value of `this` inside a normal (non-arrow) function?
Hints without spoilers, and a plain explanation of why a wrong answer was wrong — answered only from this lesson. Free account, no card.
Sign up freeWhat interviewers actually ask about this, and what a strong answer has to cover.
A strong answer covers
Likely follow-ups
Here's the twist that trips everyone: variables are lexical (decided by where you write them), but this is dynamic — decided by how the function is called, at the call site.
Same function, called four ways, four different values of this.
function who() { return this?.name ?? "none"; }
const user = { name: "Adesh", who };
who(); // "none" — plain call (this is undefined in strict mode)
user.who(); // "Adesh" — implicit: called on an object
who.call(user); // "Adesh" — explicit: you pass this in
const bound = who.bind(user);
bound(); // "Adesh" — bound: this is locked in
this isn't fixed when a function is written — it's decided by HOW it's called, at the call site.
Arrow functions are the exception: they don't get their own this — they borrow it from where they were written. That's lexical, like a variable.
The classic bug: passing a method as a callback loses its object.
const timer = {
seconds: 0,
tick() { this.seconds++; },
};
setInterval(timer.tick, 1000); // broken: `this` is no longer `timer`
setInterval(() => timer.tick(), 1000); // fixed: the arrow keeps the call on `timer`
this is undefined in strict mode — not the surrounding object.this, not the object.bind returns a new function — it never changes the original.this is the dynamic counterpart to lexical scope — knowing both, and when arrow functions opt out of dynamic this, is the whole topic. It's also why methods often needed binding in older React class components.