r/learnjavascript • u/OrangeObvious8498 • Feb 17 '26
this keyword learning
this keyword in javascript is very confusing especially during interviews. Everytime, its very confusing to understand and easily to forget.
What would be the best resource to learn this?
4
Upvotes
View all comments
Show parent comment
2
u/senocular Feb 17 '26
There can be "... except when it isn't." exceptions to a lot of these rules too ;)
Except when a super constructor returns a different object.
This is always true. The tricky part here can be what the surrounding scope is. Consider:
The above is more about the weirdness of
classsyntax than arrow functions.Except in ES modules where it is
undefinedand CJS modules where its theexportsobject.Except when the function is bound (covered in 5) or an unqualified method called within a
withblock.Use of
withblocks are rare except in maybe legacy code. They're deprecated and even disallowed entirely in strict mode. However, if you happen to put JavaScript code in HTML attributes (like<button onclick="">, also rare, hopefully), they're run inwith-like object scopes which behave the same way.Except if the function is an arrow function (or called with
newor pre-bound).The arrow (and
new) case might not apply if following the rules in order of importance and hitting one of those before making it here. Multiple-bind calls is a little tricky where only the first boundthisis applied. Similarly, neither call nor apply can overridethisfor a function created with bind.And there are even other, more obscure cases than these.
thisis a wild thing.