I'm trying to use Closure in JS in order to declare a function named **expandArray()** which contain an Array named **myArray**and Returns an anonymous function that directly modifies myArray by increase the values by 1 than the returned function then returns the value of **myArray**. My Problem here one the last part where the returned function return a function not Array value ?!
This is my code
function expandArray() {
const myArray = [1, 1, 1];
return function () {
myArray.forEach( function (num, index, myArray) {
myArray[index] = num + 1;
});
return myArray;
};
}
console.log(expandArray());
mapdoesn't mutate, it returns a new array, writereturn myArray.map, not the unmodified myArray.map()is only used to iterate over the elements in the array. The returned array will only containundefinedsexpandArray()()expandArrayfunction returns a function. And running that function will return a reference to the internal array.