javascript foreach function this parameter

Javascript forEach Function This Parameter

If you are working with arrays in Javascript, you might have come across the forEach function. The forEach function is used to loop through the elements of an array and perform a function on each element.

The forEach function takes a callback function as an argument. This callback function is executed for each element of the array. The forEach function also has an optional parameter, known as the this parameter.

What is the this parameter?

The this parameter is used to set the value of the "this" keyword within the callback function. The "this" keyword refers to the object that the function belongs to.

When you pass a value for the this parameter to the forEach function, it sets the value of "this" to that value within the callback function.

Example

Let me give you an example to help you understand how the this parameter works with the forEach function.


const person = {
  name: 'John',
  age: 25,
  hobbies: ['reading', 'hiking', 'coding'],
  printHobbies: function() {
    this.hobbies.forEach(function(hobby) {
      console.log(this.name + ' likes ' + hobby);
    }, this);
  }
};

person.printHobbies();

In this example, we have an object called "person" with a name, age, and hobbies. The object also has a method called "printHobbies" which uses the forEach function to loop through the hobbies array and print out each hobby.

Within the callback function of the forEach loop, we use the "this" keyword to refer to the person object. However, because the callback function has its own execution context, the "this" keyword actually refers to the global object and not the person object.

To fix this issue, we pass the person object as the value for the this parameter of the forEach function. This ensures that the "this" keyword within the callback function refers to the person object.

Other Ways to Use this Parameter

There are other ways you can use the this parameter with the forEach function. For example, you can set the value of "this" to a different object:


const obj = {x: 1};

[1, 2, 3].forEach(function(element) {
  console.log(this.x + element);
}, obj);

In this example, we set the value of "this" to the "obj" object. Within the callback function of the forEach loop, we use the "this" keyword to access the "x" property of the "obj" object.

You can also use arrow functions instead of regular functions to automatically bind the "this" keyword to the outer execution context:


const person = {
  name: 'John',
  age: 25,
  hobbies: ['reading', 'hiking', 'coding'],
  printHobbies: function() {
    this.hobbies.forEach((hobby) => {
      console.log(this.name + ' likes ' + hobby);
    });
  }
};

person.printHobbies();

In this example, we use an arrow function as the callback function for the forEach loop. Arrow functions automatically bind the "this" keyword to the outer execution context, so we don't need to pass a value for the this parameter.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe