Binding of ‘this’ in Javascript

In JavaScript, the ‘this’ keyword plays a crucial role in determining the context within which a function is executed. Understanding and effectively managing ‘this’ is important for writing clean and maintainable code in JS. In this blog, we will dive deep into the concept of ‘this’ and explore various techniques for binding it in different scenarios. Here, We are going to explain- what are the ways of javascript this binding. We will start from basics which means you first need to understand what is binding in Javascript. By the end, you will have a clear understanding of how ‘this’ works and be equipped with the knowledge to control its behavior in your JavaScript applications.

What is Binding?

Binding in JavaScript (Javascript bind) describes the process of connecting a value to a specific variable or function. It determines the scope and accessibility of variables and the behavior of functions. In simple terms, It determines how the ‘this’ keyword behaves within a function and allows you to control the value of ‘this’ when the function is called. Binding is important because it influences how functions access and interact with variables, properties, and methods within different contexts.

What is ‘this’ keyword in JS?

In JavaScript, the ‘this’ keyword plays a crucial role in determining the context within which a function is executed. it refers to the object that owns or calls the currently executing function. Its value is dynamically determined at runtime based on the invocation context. Below we have mentioned the ways of ‘this’ binding in javascript.

Ways of Binding ‘this’ Keyword in JS

There are different ways of binding ‘this’ in js.

1. Implicit Binding

The most common way for ‘this’ keyword to be bound is implicity. Implicit binding is the best method of binding this in javascript. This happens when a function is called a method of an object. In this case, ‘this’ will refer to the object that the function is called on. and it is automatically done by the JS engine.

Here’s an example:

In the above example, ‘Person’ is the context, and when ‘sayName()’  function is called on the ‘Person’ object, ‘this’ refers to the whole object that is the ‘Person’ object. 

2. Explicit binding

Explicit binding is yet another way of binding this in javascript. The ‘this’ keyword can also be bound explicitly. If You want to explicitly specify the ‘this’ value for a function,  then this technique is used. It can be done using the call(), apply(), or bind() methods.

1. The call() method takes a list of arguments, and it binds ‘this’ to the first argument.

           Here’s an example:

In the above example, call() is used to invoke the ‘fun()’ function with the ‘this’ value set to the ‘person’ object. This allows us to access the ‘name’ property of the ‘person’ object within the ‘fun’ function.

We can also pass additional arguments to the function using call() :

Here, the second argument of ‘call()’ is used to pass the message argument to the ‘call()’ function.

2. The apply() method is similar to the call() method, but it takes an array of arguments instead of a list of arguments.

Here’s an example:

In the above example, apply() is used to invoke the ‘fun()’ function with ‘this’ value set to the ‘person’ object. The ‘message’ array is passed as the second argument, which is then spread into individual arguments for the ‘fun()’ function.

3. The javascript bind() methods creates a new function with a bound ‘this’ value and any additional arguments provided. The original function is not modified.

  Here’s an example:

In  the above example, bind() is used to create a new function ‘greetPerson’ with ‘this’ value set to the ‘person’ object. The ‘greetPerson’ function can be invoked separately. We can also present arguments using bind().

3. Using ‘new’ Keyword

When a function is used with the ‘new’ keyword, it is called as a constructor function and ‘this’ keyword inside the function is bound to the newly created object. This means that any references to ‘this’ inside the function will refer to the new object.

Here’s an example:

In the above example, The ‘this’ keyword inside ‘car’ function will refer to the ‘myCar’ object, so the make, model, and year properties will be set on the myCar object.

4. Default Binding

in javaScript, when a function is called without any special syntax, ‘this’ refers to the global object. In a browser, the global object is window.

Here’s an example:

In the above example, ’this’ keyword is not resolved with any of the bindings, implicit , explicit, or  ‘new’, then the ‘this’ is bound to the window(global) object.


Understanding the concept of binding and knowing how to control the behavior of ‘this’ is crucial in JavaScript to ensure the proper functioning and avoid unexpected results when working with functions and objects. Above, we have explained the binding in Javascript of “this” keyword. Here are the highlights of the same:

  • In the case of implicit binding, this binds to the object adjacent to the dot(.) operator while invoking the method.
  • In the case of explicit binding, we can call a function with an object when the function is outside of the execution context of the object. The methods call(), apply(), and bind() play a big role here.
  • When a function is invoked with the ‘new’ keyword, ‘this’ keyword inside the function binds to the new object being constructed.
  • When the ‘this’ keyword is not resolved with any of the bindings, implicit, explicit, or new, then this is bound to the window(global) object. In JavaScript’s strict mode, this will be undefined.

Recent Post

  • How to Implement File Uploads in Node.js with Multer?

    For many web applications that let users share papers, photos, and other types of material, file uploads are a necessary functionality. Multer is a well-liked middleware used for Handling file uploads in Node.js using Multer the Node.js environment to effectively handle file uploads. We’ll look at how to use Multer to create file uploads […]

  • How to Use Local Storage to Persist Data in a JavaScript Application?

    Data persistence is a key aspect of web applications. In JavaScript, one of the primary tools for achieving this is Local Storage, a part of the Web Storage API. This JavaScript data persistence tool provides a simple key-value storage mechanism within the user’s browser. Unlike session storage, which persists data only during a session, Local […]

  • How to Use WordPress API?

    WordPress stands out as a leading content management system, celebrated for its dynamic capabilities in website and blog development. Renowned for its rich feature set and extensive customization options, WordPress empowers creators to fashion captivating online experiences. In recent times, WordPress has expanded its horizons into the realm of APIs, granting developers a potent tool […]

  • How to Use Post and Pre Hooks in Mongoose?

    In Mongoose, a widely-used library for working with MongoDB, there are valuable tools called “Pre and Post Hooks.” These hooks, also known as “Mongoose Middleware” or “Mongoose Model Hooks,” allow developers to run their own code just before or after specific actions on the database. With “Pre Hooks,” you can customize actions before they happen, […]

  • How To Create Reusable Components in React: Best Practices

    Rеact is a popular library for building usеr intеrfacеs with JavaScript. One of the main benefits of Rеact is its ability to create reusable componеnts that can be used in different parts of your application. Reusable componеnts can help you savе timе, rеducе codе duplication, and еnsurе consistеncy in your UI. However, creating rеusablе componеnts […]

  • Implementing Higher Order Components in Class-Based Components for Code Reusability in ReactJS

    Rеact is a powerful library for building usеr intеrfacеs, and one of its corе strengths liеs in its componеnt-basеd architеcturе. Componеnts arе thе building blocks of Rеact applications, and thеy allow you to crеatе modular and rеusablе piеcеs of UI. Howеvеr, as your application grows, you may find thе nееd for rеusing cеrtain functionalitiеs across […]