The JavaScript this Keyword



What is this?

In JavaScript, when we use the this keyword, it points to an object.

The object that is influenced by howthis is employed or called depends on the context of its invocation.

The termthis in programming points to various objects based on its usage.

In a method related to an object, the term this points to the specific object.
By itself, the term this points to the overall object, often known as the global object.
In a function, the term this points to the main object.
In a function under strict mode, the value of this becomes undefined.
At an occasion, the term this signifies the element that encountered the event.
The functions call(), apply(), and bind() can change the reference of this to different objects.

Note

this isn't like a variable; it's a keyword. You can't modify the value of this.

this in a Method

When employed in a method of an object, the term this points to the specific object.

On the example at the top of this page, when you see this, it is pointing to the person object.

The fullName method belongs to the person object.


this Alone

When used by itself, the term this points to the overarching or main object known as the global object.

Since this operates in the overall scope.

In a web browser, the global object is represented as [object Window] in the browser window:

 In strict mode, when used alone, this also refers to the global object:


this in a Function (Default)

In a function, the main object that is automatically linked to the keyword this is called the global object.

In a web browser, the global object appears as [object Window].


this in a Function (Strict)

In JavaScript, when you use strict mode, it doesn't permit default binding.

When utilized within a function under strict mode, the value of this becomes undefined.


this in Event Handlers

In HTML event handlers, the term "this" points to the HTML element that got the event.


Object Method Binding

In these instances, the this represents the person object:

In simpler terms, this.firstName refers to the firstName attribute of the this object, specifically the person object.


Explicit Function Binding

The JavaScript methods call() and apply() are already built-in functions.

Both can be used to invoke a method of an object by providing another object as an argument.

The provided example demonstrates calling the fullName method of person1 with person2 as an argument. In this context, when this is used, it points to person2, even though fullName is originally a method of person1.


Function Borrowing

The bind() method allows an object to use a method from another object.

This illustration generates two items, namely a person and a member.

The member object uses the fullname method from the person object.


This Precedence

To find out what object is referred to by this, follow this order of precedence.

PrecedenceObject
1bind()
2apply() and call()
3Object method
4Global scope

Is this inside a function that's being called with bind()?

Does this get invoked within a function using apply()?

Does this get executed within a function when using call()?

Is this part of an object's method?

Is this within a function in the global scope?