Is it possible to change values of the array when doing foreach() in javascript?

Yes, it is possible to change array values during forEach() iteration in JavaScript. Unlike some programming languages where modifying arrays during iteration is restricted, JavaScript's forEach() allows you to modify the original array by using the array parameter or accessing the array directly by index.

The forEach Method

The forEach() method executes a provided function once for each array element. It passes three parameters to the callback function: the current element value, the element's index, and the array itself. You can use the array parameter to modify elements during iteration.

Syntax

array.forEach(function(value, index, array) {
    // Your code here
    // Modify array[index] to change values
});

Where:

  • value ? The current element's value being processed

  • index ? The index of the current element (optional)

  • array ? The array that forEach() is being applied to (optional)

Example 1: Adding Prefix to String Elements

This example demonstrates how to modify string array elements by adding a prefix:

let employees = ['Abdul', 'Yadav', 'Badavath', 'Jason'];
console.log("Original array:", employees);

employees.forEach(function(item, index, array) {
    array[index] = 'Intern-Software Engineer ' + item;
});

console.log("Modified array:", employees);
Original array: [ 'Abdul', 'Yadav', 'Badavath', 'Jason' ]
Modified array: [
  'Intern-Software Engineer Abdul',
  'Intern-Software Engineer Yadav',
  'Intern-Software Engineer Badavath',
  'Intern-Software Engineer Jason'
]

Example 2: Converting to Title Case

Here we convert lowercase strings to title case by modifying each element:

let names = ['abdul', 'yadav', 'badavath', 'jason'];
console.log("Original array:", names);

names.forEach(function(item, index, array) {
    array[index] = item[0].toUpperCase() + item.substring(1);
});

console.log("Modified array:", names);
Original array: [ 'abdul', 'yadav', 'badavath', 'jason' ]
Modified array: [ 'Abdul', 'Yadav', 'Badavath', 'Jason' ]

Example 3: Squaring Numeric Values

This example replaces each number with its square value using an arrow function:

let numbers = [1, 2, 3, 4];
console.log("Original array:", numbers);

numbers.forEach((value, index) => numbers[index] = value * value);

console.log("Modified array:", numbers);
Original array: [ 1, 2, 3, 4 ]
Modified array: [ 1, 4, 9, 16 ]

Key Points

  • You can modify array elements using array[index] = newValue inside forEach()

  • Changes affect the original array directly

  • Use the third parameter (array) or access the array directly by variable name

  • Consider using map() if you need to create a new array with transformed values instead of modifying the original

Conclusion

forEach() allows you to modify array elements during iteration by accessing elements through their index. This makes it a powerful tool for in-place array transformations when you need to update the original array rather than create a new one.

Updated on: 2026-03-15T20:02:29+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements