Skip to content
the poor coder

javascript foreach function this parameter

Poor Man
Published
Reading time
2 min read
On this page

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.

Text

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:

Text

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:

Text

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.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC