Learn JavaScript
this keyword in JavaScript
40.1
Lesson

this

In JavaScript, this is a special keyword. When we use it in a piece of code, it refers to the ‘object’ that is calling the code.

In the example given below, we are using this inside the method fullName. The person object contains the fullName method. Here, this refers to the person object.

The value of this also depends on where we are using it.

  1. If we use this in the global scope, this refers to the global object.

  2. When we use it inside a function in the global scope, this refers to the global object. However, if strict mode is being used, then this would be undefined.

  3. When we use it inside a function of a class, this refers to the instance of the class calling that function.

  4. When we use it inside the method of an object, this refers to that object.