Master JavaScript Programming From Scratch

Clear, interactive, and structured JavaScript lessons designed for absolute beginners.

this Keyword

Learn how the JavaScript this keyword refers to the object on which a method is called and how it helps methods work with object properties.

What is the this Keyword?

The this keyword is a special JavaScript keyword used inside functions and methods.

When a regular function is called as an object method, this refers to the object on which that method was called. :chatgpt-content-reference{index="1"}

Simple Rule

When we write: object.method()

Inside that regular method, this refers to object.

JavaScript this keyword inside object method

The this keyword inside an object method refers to the object on which that method is called.

Understanding this

this
Current Object

Inside a regular object method, this refers to the object used to call that method.

this.name
Access Property

We can use this to access properties belonging to the current object.

this.course
Reusable Method

The same method logic can work with different objects and their data.

How this Works Inside an Object Method
learner Object
→
greet() Method Call
→
this Refers to learner
→
this.name Object Property

In learner.greet(), the object before the dot becomes the value of this inside the regular method.

Example 1: Accessing an Object Property

The this keyword can be used to access a property of the current object.

let learner = {

    name: "Rahul",

    introduce: function () {
        console.log("My name is " + this.name);
    }

};

learner.introduce();
Output: My name is Rahul

Example 2: Accessing Multiple Properties

A method can use this to access multiple properties of the same object.

let course = {

    title: "JavaScript",
    duration: "3 Months",

    showInfo: function () {
        console.log(this.title);
        console.log(this.duration);
    }

};

course.showInfo();
Output: JavaScript
Output: 3 Months

Example 3: Same Method with Different Objects

The value of this depends on which object calls the method. The same function can therefore work with different objects.

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

let trainer = {
    name: "Sneha",
    showName: showName
};

let developer = {
    name: "Vikram",
    showName: showName
};

trainer.showName();
developer.showName();
Output: Sneha
Output: Vikram

Example 4: Using this to Calculate a Value

The this keyword can also be used to perform calculations using values stored inside the current object.

let product = {

    price: 500,
    quantity: 3,

    getTotal: function () {
        return this.price * this.quantity;
    }

};

console.log(product.getTotal());
Output: 1500
Important

Do not think of this as always meaning one fixed object. Its value depends on how the function is called. :chatgpt-content-reference{index="2"}

For the object-method examples on this page, remember this simple pattern: object.method() → this = object.

Summary

The this keyword provides a way for a regular object method to refer to the object on which the method is called. It can be used to access properties such as this.name, this.course, and this.price. Its value depends on how the function is invoked. :chatgpt-content-reference{index="3"}