JavaScript, when is this?
May 2, 2025

For me, the trouble was always that this is contextual, but that context isn’t meaningful to us developers so much as it’s meaningful to JavaScript. You and I are used to telling JavaScript what things are: this is the identifier we typed, where we typed it, scoped to the pair of brackets we typed around it, and it has the value we assigned to it.
That’s not the case with this, which is—brace for it—a keyword that refers to the object bound to the function where this is invoked at the time when that function is called.
That is an absolute nightmare of a sentence, I know, but bear with me. It breaks down to two important concepts: this references the object bound to a function, and the object referenced by this is determined at the time a function is called. Once you’ve got a handle on those two aspects, this is— well, still a little weird, but the knowable kind of weird.
Source: JavaScript, when is this?
An excellent exploration of `this` in JavaScript.