This Keyword - Essential JavaScript Concepts

1 minute read

‘This’ keyword is widely used in almost all programming languages. Like in some programming languages This variable points to instance of an object or class. But in JavaScript it has bit different meaning; here This refers to Calling Context of the function. Whoever is calling a function, the calling context of that function is known as This.

It is very nicely explained with a simple DOG & CAT example in our video tutorial JavaScript Concepts: What Is this Keyword | 0004-08 Suppose we have DOG class and it has its Speak method, similarly in CAT class we have its own Speak method. Anytime if we create an instance of CAT and call speak function, it will always give output in context of CAT in most of the server side programming languages like C#, Java etc. But in JavaScript we can change context at run time like we can call context of CAT in DOG Instance. This is very powerful concept of JavaScript which makes it different from other programming languages.

In subsequent videos we will learn how we can change context of function in various different ways in JavaScript. Snapshot from our video session:

The best way to understand the content of this article is by quickly executing the code snippet in browser’s console. In our GitHub repository, this concept is explained with real world example of app installation. Please visit our GitHub code base https://github.com/rupeshtiwari/javascript-concepts to get the code snippet for this article.

Comments