-1

What is the difference between globalArray and foreachArray object in this below code? and which scenario we need to use the foreachArray object instead of globalArray?

var globalArray= ['Apple', 'Banana'];


globalArray.forEach(function(item, index, foreachArray) {
  console.log(foreachArray);// result is ['Apple', 'Banana'];
  console.log(globalArray);// result is ['Apple', 'Banana'];
});

8
  • 4
    The last argument passed in to forEach() is the array that's being iterated itself. That's very useful if you want to follow a pure-function approach and not rely on "external" (accessible-via-closure) variables. Commented Jul 26, 2018 at 11:08
  • developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Jul 26, 2018 at 11:08
  • I t think they are the same reference. Commented Jul 26, 2018 at 11:08
  • @haim770 what you mean by follow a pure-function approach?? Commented Jul 26, 2018 at 11:13
  • 1
    @RameshRajendran ['Apple', 'Banana'].forEach Commented Jul 26, 2018 at 11:13

4 Answers 4

4

The forEach function accepts 3 Parameters

currentValue The value of the current element being processed in the array.
index (Optional) The index of the current element being processed in the array.
array (Optional) The array that forEach() is being applied to.

Your question is how is the last parameter connects to the array being iterated through but also why you would want this.

The parameter is actually a reference to the original array

let arrayVar = [0];

arrayVar.forEach((currentValue, index, array) => {
  console.log(arrayVar === array)
})

But why is this useful? The three main reasons for this are:

  • Referencing an array without a variable
  • Passing the array to an external function
  • Keeping your code tidy and concise

Reference an Array that has not been saved as a variable which may come in handy if the array is only going to be used once.

[1,2,3,4].forEach((value,index,array)=>{
  console.log(array);
})

Passing your forEach to an external function this can be incredibly helpful when you want to use the same function on several arrays.

function extFunct(value, index, array) {
  console.log(array)
}

let array = [1, 2, 3, 4];
let array2 = [5, 6, 7, 8];

array.forEach(extFunct);
array2.forEach(extFunct);

Keeping code tidy and concise you can reference your array without using the long clear array name.

let someLongDescriptiveArrayName = [1,2,3,4];

someLongDescriptiveArrayName.forEach((v,i,a)=>{
  console.log(a)
})

I hope that all makes sense, if not feel free to ask any questions.

Sign up to request clarification or add additional context in comments.

Comments

3

They are 2 variable with same reference, only variable names are changed. See below, if I push an item in foreachArray, it add same in globalArray as well.

var globalArray= ['Apple', 'Banana'];


globalArray.forEach(function(item, index, foreachArray) {
  foreachArray.push('Orange');
  console.log(foreachArray);// result is ['Apple', 'Banana', 'Orange'];
  console.log(globalArray);// result is ['Apple', 'Banana', 'Orange'];
});

Check here the scenario when should it be used: Why provide an array argument in Javascript's array.forEach callback?

1 Comment

guys I also asked which scenario we need to use?
2

They are same(they refer the same object). According to the docs:

array(In your case foreachArray) is the array that forEach() is being applied to.

This is the signature of forEach:

someArray.forEach(function callback(currentValue[, index[, array]])   

Where:

currentValue: The value of the current element being processed in the array.

index(Optional): The index of the current element being processed in the array.

array(Optional): The array that forEach() is being applied to.

In your here someArray & array will be same just like in your case globalArray & foreachArray are same.

So, if you need to refer the array on which forEach is being called use foreachArray as it is an language feature provided by JavaScript.

1 Comment

guys I also asked which scenario we need to use?
1

globalArray and foreachArray object are the same.

you are passing foreachArray as to the third parameter.

Third parameter array: The array that forEach() is being applied to.

And it points the same reference too.

var globalArray= ['Apple', 'Banana'];


globalArray.forEach(function(item, index, foreachArray) {
  foreachArray.push("Cherry");
  console.log("foreachArray: ", foreachArray);// result is ['Apple', 'Banana'];
  console.log("globalArray..", globalArray);// result is ['Apple', 'Banana'];
});

2 Comments

guys I also asked which scenario we need to use?
There is no difference between these two objects. So, you can use any of them. But I prefer to use foreachArray as per the documentation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.