Course contentsShow
← JavaScript
Lesson 544 of 2,72314. The 'this' KeywordFree lesson

'this' Is Not Self-Reference by Default

Clarifying that 'this' doesn't automatically refer to the function itself—a common misconception.

'this' Is Not Self-Reference by Default

You'll learn: Why this inside a function doesn't automatically point to the function itself—a mistake many beginners make.

The Common Misconception

When you write a regular function in JavaScript, it's tempting to think that this inside that function refers to the function itself. After all, you're inside the function, right? Unfortunately, that's not how this works.

The reality: this is determined by how the function is called, not where it's defined. It doesn't create a self-reference automatically.

Why This Confuses People

In many programming languages, there's a keyword that lets a function refer to itself (like self in Python classes). Beginners sometimes assume this serves that purpose in JavaScript functions. It doesn't—at least not by default.

If you want a function to reference itself, you should use its actual name or the arguments.callee property (though that's deprecated in strict mode). The this keyword serves a different purpose: it points to the execution context, which changes based on how you call the function.

Think of It This Way

Imagine you're an actor reading lines from a script. The script says "I love this place." The word "this" doesn't refer to the script itself—it refers to whatever location makes sense in the scene you're currently performing. Similarly, this in a function doesn't point to the function—it points to whatever context is relevant when the function runs.

Example of the Confusion

Without running code, imagine a function that tries to count how many times it's been called by storing this.count. If you think this means the function itself, you'd expect that to work. It usually won't—this will point somewhere else entirely, depending on how you called the function.

Key Takeaway: this does not automatically create a self-reference to the function; it's determined by the function's call-site, not its definition.