Skip to main content

Command Palette

Search for a command to run...

Understanding this in JavaScript (Simple Guide for Beginners)

Learn how this works in different situations with clear and easy examples

Updated
4 min read
Understanding this in JavaScript (Simple Guide for Beginners)

JavaScript can feel confusing at times, and one of the most common points where beginners get stuck is the this keyword.

At first glance, this seems simple. But when you start using it in different places, its value changes and that’s where confusion begins.


]What does this represent?

In JavaScript, this represents the object that is calling the function.

Simple rule:
"Who is calling the function?" → that is this


this in Global Context

When you use this outside of any function or object, it refers to the global object.

  • In browsers → window

  • In Node.js → global

Example:

console.log(this);

In a browser, this will print:

window

So here, this is the global object.


this Inside Objects

When a function is inside an object (called a method), this refers to that object.

Example:

const user = {
  name: "Koushik",
  greet: function () {
    console.log(this.name);
  }
};

user.greet();

Output:

Koushik

Why?

Because user is calling the function, so this = user.


this Inside Functions

Now things get a bit tricky.

Normal Function (Non-strict mode)

function show() {
  console.log(this);
}

show();

Output (in browser):

window

Because no object is calling it so it defaults to global.


Strict Mode

"use strict";

function show() {
  console.log(this);
}

show();

Output:

undefined

In strict mode, this is not automatically set to the global object.


How Calling Context Changes this

This is the most important part.

this does NOT depend on where the function is written.

It depends on how the function is called.


Example 1: Same function, different caller

function greet() {
  console.log(this.name);
}

const person1 = { name: "Kousik", greet };
const person2 = { name: "Rahul", greet };

person1.greet(); // Kousik
person2.greet(); // Rahul
  • Same function

  • Diffrent callers

  • Diffrent this


Example 2: Losing this

const user = {
  name: "Koushik",
  greet: function () {
    console.log(this.name);
  }
};

const fn = user.greet;
fn();

Output:

undefined

Because now it's just a normal function call — no object is calling it.


Quick Summary Table

Situation Value of this
Global scope Global object (window)
Inside object method The object itself
Normal function Global object / undefined (strict mode)
Same function, different object Depends on caller

Real Life Way to Understand this

Think of this like a name tag.

Whoever is calling the function is wearing the name tag.

So when the function runs, it checks:

  • "Who called me?"

  • That person becomes this


Common Mistake Beginners Make

Thinking this depends on where the function is written

t actually depends on how it is called


Final Thoughts

Understanding this is a big step in mastering JavaScript.

At first, it may feel confusing, but once you remember this one rule:

"this = the caller of the function"

Everything starts to make sense.

Practice with small examples, try changing the caller, and observe how this changes.

That’s the best way to learn.

Conclusion

The this keyword in JavaScript may seem confusing at first, but it becomes much easier once you understand one simple idea: it depends on who is calling the function. By practicing different examples and observing how the caller changes, you can quickly build a strong understanding of this. Keep experimenting, and over time it will feel natural.