Understanding the Basic Concept of the "this" Keyword in JavaScript

Welcome to the "Advanced JavaScript" article series. In this article series we have covered many topics in JavaScript. You can read the complete series by navigating the following links.

This is the article where we will try to understand the the "this" keyword and we will try to be a master of it. So, let's try to explore the "this" keyword.

I will expect that you have experience in some other programming language (like C++ , C# and so on) and from there you get the basic idea of the "this" keyword. Anyway, if you don't then don't worry. The purpose of this article is to explain the "this" keyword from scratch. Let's read a few sentences in English.

"Sourav is writing 'this' article. 'This' is the language that I like most." So, what have we learned here? In the first sentence "this" refers to the current article and in the second sentence "this" refers to the current programming language.

Language developers are too wise. Ha Ha. They don't want to confuse us at all. Yes, the "this" keyword always points to the current object. So, the concept is that when we specify the "this" keyword it will always point to the current working object. Now to understand this with an example.

The "this" keyword within a class/object

In this example we will create one class (don't be confused, if you are new to this series then please go through the class section of this series) with a name and surname property. The beauty of the class is that the argument allies and class property allies are the same and to distinguish them we have used the "this" keyword. In this scenario "this" is pointing to the class property. Have a look at the following example.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
     <title></title>
</head>
<body>
     <script>
         var person = {
            name: '',
            surname: '',
            print: function (name, surname) {
                 this.name = name;
                 this.surname = surname;
                console.log(this.name + " " + this.surname);
            }
        }
        person.print('sourav', 'Kayal');
     </script>
</body>
</html>

Here is the output from the code above.

this keyword with in class
 

The "this" keyword to point to the class property

Though this example is very similar to the previous one, it will help to clarify your confusion, if you have any. In this example we are setting the property value of the class and in the case of the function call we are sending another value. Have a look, when we are printing the value of a variable using the "this" keyword , it's always taking the property value that we set by default.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
     <title></title>
     <!--<script src="backbone/Jquery.js" type="text/javascript"></script>-->
</head>
<body>
     <script>
         var person = {
            name: 'Sourav',
            surname: 'Kayal',
            print: function (name, surname) {
                console.log("Name in class:- " + this.name);
                console.log("Name in parameter :- " + name);
            }
        }
        person.print('Ajay', 'Joshi');
     </script>
</body>
</html> 

This is the sample output.

this keyword withclass property

The "this" points to the global object (window)

Let's understand the concept. The "this" keyword always points to the global object, if we do not specify the full namespace. In this example we have declared two variables that with the same name. One variable is global and another is local to the function. When we call the variable using the "this" keyword it's pointing to the global one and to print the local one, the variable name is enough.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
     <script>
         var value = 100;
         var fun = function () {
             var value = 200;
            console.log("pointed by this(window object):- " + this.value);
            console.log("Local value:- " + value);
        } 
        window.fun();
     </script>
</body>
</html>

Here is sample output of this code.  

this points to global object

Accessing local variable using the "this" keyword

In the example above we saw that, if we specify the "this" keyword then it points to the global object, always. Now, the question is, shall we point to the local object/variable using "this" keyword? Yes, we can. Here is am example.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
     <script>
         var name = 'Sourav';
         var surname = 'Kayal';
         function showInfo() {
            console.log(this.name + " " + this.surname);
        }
          var person = {

           name: 'Ajay',
            surname: 'Joshi',
            showInfo: function(){
                console.log(this.name + " " + this.surname);
                }
        }
        showInfo(); //Sourav Kayal
        person.showInfo(); //Ajay Joshi       
     </script>
</body>
</html>

The point to be noted at the time of the call to the "showInfo" function in the second case is taht we are specifying the object/class name (person). And hence this object is pointing to the local resource. Here is the evidence.

local variable using this keyword

Conclusion

Now a days, writing show article, Ha Ha, due to the preparation of another series. Any way, I hope this article has provided a solid understanding of the "this" keyword in JavaScript. And I hope that in the future you will never be confused by the "this" keyword.
Finally,
"this" is the explanation of the "this" keyword in "this" article. Never be confused by "this" after reading "this" article. Now, I hope "this" concept in JavaScript is clear in "this" article. "this" is the time to say good bye.

Up Next
    Ebook Download
    View all
    Learn
    View all