r/learnjavascript • u/dotpr • Dec 18 '22
Cannot understand "this" keyword
My head is going to explode because of this. I watched several videos, read articles from MDN, W3schools, and TOP, and I still can't understand.
There's so many values and scenarios around it and I feel like they're explained so vaguely! I struggle to get familiar with it. Can someone drop their own explanation?
89
Upvotes
View all comments
Show parent comment
2
u/delventhalz Dec 18 '22
All functions are lexically scoped. This is why you can reference variables in wrapping scopes within functions.
The difference with arrow functions is just that they literally have no
thisdefined, unlikefunctionfunctions which always have athiseven if the value of it isundefined.The effect of that behavior is that if you reference
thiswithin an arrow function it will behave like any variable not defined in a nested scope. JS will go looking for the variable in a wrapping (lexical) scope.