JavaScriptEasy
How is the value of this determined?
By FrontendPro Editorial Team Updated 9/26/2026
#JavaScript
Answer
this is not decided by where a function is written — it is decided by how the function is called. The same function can see a different this on every call. Work through these rules in order; the first one that matches wins.
- Arrow function — it has no
thisof its own and takes the one from the scope where it was defined. This is fixed at creation and nothing can change it, not evencall. - Called with
new—thisis the brand-new instance being constructed. - Called with
call,apply, or bound withbind—thisis the object you passed. - Called as a method,
obj.fn()—thisisobj, the thing to the left of the dot. - Called plainly,
fn()—thisisundefinedin strict mode and modules, or the global object (window) otherwise.
js
const user = {
name: "Mai",
greet() { return `Hi ${this.name}`; },
};
user.greet(); // "Hi Mai" — rule 4
const fn = user.greet;
fn(); // TypeError — rule 5, `this` undefined
fn.call({ name: "Nam" }); // "Hi Nam" — rule 3
Where it usually goes wrong
Passing a method as a callback drops the dot, and with it rule 4:
js
setTimeout(user.greet, 100); // `this` lost
setTimeout(() => user.greet(), 100); // fine — the call keeps the dot
setTimeout(user.greet.bind(user), 100); // also fine
One more trap: in a DOM event handler a regular function gets the element as this, but an arrow function does not — it keeps the enclosing scope's value. Use event.currentTarget when you want the element regardless of which style you chose.
