If you’ve ever seen the word this in JavaScript and felt a little confused, you’re not alone! this can seem tricky at first because it doesn’t always mean the same thing — it depends on where and how you use it. But don’t worry! In this blog, we’ll explain this in a simple way with easy examples, so you can understand it clearly and use it without any confusion.

In JavaScript, the this keyword refers to the object that is currently executing the function. In simple words, this represents who is calling the function. Depending on how the function is called, this can point to different objects like a regular object, the window object in browsers, or even undefined in strict mode.
The behavior of this in JavaScript can change depending on where and how it is used. When used inside an object, this usually refers to that object itself. But when used inside a regular function (not inside an object), this behaves differently — it typically refers to the global object (window in browsers) or undefined in strict mode. Understanding this difference is very important because it helps you write correct and predictable code.
How this Works Inside an Object?
Here’s a simple explanation you can write for this code to explain how this behaves inside an object:
When this is used inside an object’s method (a function inside an object), it refers to the object itself. In the example below, this inside the getCityName Function refers to the address object:
const address = {
name: "Rahul",
getCityName: function () {
console.log(this);
}
}
address.getCityName(); // Output: address objectWhen we call address.getCityName(), the object address is the one calling the method. So, inside getCityName, this points to address, and if you console.log(this), it will print the entire address object.
In JavaScript, when you call a function as a method of an object (using dot notation like object.function()), this inside that function automatically refers to the object before the dot.
address.getStreetName = function () {
console.log(this);
}
address.getStreetName(); // Output: address objectEven though getStreetName was added later, when you call address.getStreetName(),
addressis the object calling the function.- So, inside
getStreetName,thiswill again point to theaddressobject. - As a result,
console.log(this)will print the completeaddressobject.
✅ It behaves exactly the same way as getCityName, because it is still being called by the object address.
In short:
➡️ Who calls the function decides what this refers to.
➡️ Here, address calls the function, so this = address.
How this Works Inside the Function?
Here’s a simple explanation you can use for how this behaves in a regular function (outside an object):
When a regular function is called without any object (just by its name), the value of this depends on how the code is running:
- In normal mode (non-strict mode),
thisinside a function refers to the global object.- In browsers, the global object is
window.
- In browsers, the global object is
- In strict mode (
'use strict';),thiswill beundefined.
function getCityName() {
console.log(this);
}
getCityName(); // Output: window object (in browsers)In this case, getCityName is called normally — no object is calling it. So by default, in a browser environment:
thisrefers to thewindowobject.console.log(this)will print thewindowobject (a huge object with many properties and methods).
Important Tip:
If you add 'use strict'; at the top of your code, the same function will print undefined instead of window because strict mode disables automatic binding to the global object.
'use strict';
function getCityName() {
console.log(this);
}
getCityName(); // Output: undefined✅ In short:
- In normal mode →
thisinside a function =window. - In strict mode →
thisinside a function =undefined.
How this Works Inside the Constructor Function?
In JavaScript, when you use a function as a constructor (by calling it with the new keyword), this behaves differently.
Here’s how it works:
- When you call a function with
new, JavaScript creates a new empty object. - Inside the constructor function,
thisautomatically points to that new object. - You can then add properties and methods to
this, and the new object will be returned.
function getCityName(city) {
this.city = city;
console.log(this);
}
const city = new getCityName("Hyderabad");What happens here:
new getCityName("Hyderabad")creates a new object.- Inside the function,
this.city = city;adds acityproperty to the new object. console.log(this)prints the new object, which looks like:{ city: "Hyderabad" }- Finally, this new object is stored in the variable
city.
✅ In short:
- Using
newchanges the behavior ofthis. thispoints to a newly created object.- You can attach properties to
this, and the new object is returned automatically.
Frequently Asked Questions (FAQs):
- What is the
thiskeyword in JavaScript? - How does
thisbehave inside an object method? - What does
thisrefer to inside a regular function? - How does
thisbehave in a constructor function? - Does
thisbehave differently in arrow functions?