Facebook Pixel
Step-by-Step Guide

How to Understand the 'this' Keyword in JavaScript

A step-by-step guide on how the value of 'this' is determined in different contexts including regular functions, arrow functions, classes, and event handlers.

Understand That this Is Determined at Call Time

The value of this is not fixed when a function is defined. It is determined by how the function is called at runtime. The same function can have a different value of this depending on the context in which it is invoked. This dynamic nature is the source of most this-related confusion in JavaScript.

Understand this in the Global Context

In the global execution context outside any function, this refers to the global object. In browsers, the global object is window. In Node.js, it is the global object. However, in strict mode enabled by writing 'use strict' at the top of a file, this in the global context is undefined rather than the global object.

Understand this in Regular Function Calls

When a regular function is called without any object context, this defaults to the global object in non-strict mode and undefined in strict mode. This is a common source of bugs when extracting a method from an object and calling it as a standalone function. The function loses its object context and this no longer refers to the original object.

Understand this in Method Calls

When a function is called as a method of an object using dot notation, this refers to the object that owns the method. The value of this is the object on the left side of the dot at the time of the call. If you copy that method to a variable and call it as a standalone function, this is no longer that object because dot notation is no longer used.

Understand this in Constructor Functions and Classes

When a function is called with the new keyword, JavaScript creates a brand new empty object and sets this to point to that new object inside the constructor function. After the constructor runs, the new object is automatically returned. In a class, the constructor method works the same way. this inside the class body refers to the newly created instance.

Understand this in Arrow Functions

Arrow functions do not have their own this binding. Instead, they capture the this value from their enclosing lexical scope at the time they are defined. This is called lexical this. Arrow functions are ideal for callbacks inside methods because they automatically use the this of the surrounding method without needing bind or a self variable. You cannot change the this of an arrow function with call, apply, or bind.

Use call, apply, and bind to Control this

The call method invokes a function immediately with an explicitly provided this value and individual arguments. The apply method does the same but accepts arguments as an array. The bind method does not invoke the function immediately. Instead, it returns a new function permanently bound to the provided this value. The bound function always uses that this value regardless of how it is later called.

Understand this in Event Handlers

When a function is used as a DOM event handler, this inside the function refers to the DOM element that triggered the event. If you use an arrow function as an event handler, this is not the element but instead the enclosing lexical this, often window. This distinction is important when building UI components where you need this to refer to the element to read its attributes or modify its properties.

Ready to master this completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.