WittCode💻

JavaScript this Keyword

By

Learn everything about the JavaScript this keyword and its value in object methods, function, events, special methods such as call, apply, and bind, etc.

Table of Contents 📖

What is the this Keyword?

This is a keyword, not a variable, that refers to an object. However, the object it refers to depends on how it is used. For example, this in a method is different than this in an event.

This By Itself

By itself, this refers to the global object. In other words, when this is in the global scope, it refers to the global object. In JavaScript, the global object is the window object.

console.log(this); // Window {window: Window...}

In strict mode, this still refers to the global object. We can place JavaScript in strict mode by placing the string "use strict" at the top of the page.

"use strict"; console.log(this); // Window {window: Window...}

This in an Object Method

The this keyword in an object method refers to the object.

const person = { firstName: 'Witt', lastName: 'Code', printFullName: function() { console.log(${this.firstName} ${this.lastName}); } }

person.printFullName(); // Witt Code

In this case, this refers to the person object. The person object has three properties, firstName, lastName, and printFullName. Therefore, within the printFullName method we can access the firstName and lastName properties with the this keyword.

This in an Arrow Function

Arrow functions don't have their own binding to this, instead they inherit it from the parent scope. This means that arrow function expressions are best suited for non-method functions. A non-method function is a function that doesn't belong to an object.

const myArrow = () => { console.log(this) // Window {window: Window...} }

Even in strict mode arrow functions will inherit from the parent scope.

"use strict"; const myArrow = () => { console.log(this) // Window {window: Window...} }

As arrow functions inherit this from the parent scope, using them in methods (functions belonging to an object) can cause unexpected results.

const person = { firstName: 'Witt', lastName: 'Code', printFullName: () => { console.log(${this.firstName} ${this.lastName}); }, printWindow: () => { console.log(this); } }

person.printFullName(); // undefined undefined person.printWindow(); // Window {window: Window...}

Here, printFullName and printWindow are methods (not non-methods) as they belong to the person object. However, they are created with arrow functions which don't have their own binding. Therefore, this.firstName and this.lastName will be undefined as this refers to the global object.

This in a Function Declaration

Function declarations start with the word function, similar to how variable declarations start with either var, let, or const.

function myFunctionDeclaration() { }

Within a function declaration, the this keyword refers to the global object. In a browser, the window is the global object.

function myFunctionDeclaration() { console.log(this); // Window {window: Window ...} }

The this keyword still refers to the global window object inside nested functions.

function myFunctionDeclaration() { function nested() { console.log(this); // Window {window: Window ...} } nested(); }

This type of binding is known as default binding. Default binding is what happens if we don't specify any other kind of binding.

This in a Function Declaration in Strict Mode

JavaScript's strict mode does not allow default binding. Therefore, this inside a function declaration is undefined.

"use strict"; function strictFunction() { console.log(this); // undefined }

This in Event Handlers

Event handlers are JavaScript code that handles events such as user input, actions, browser actions, etc. In event handlers, this refers to the HTML element that received the event.

Here, this refers to the HTML