Код: Выделить всё
const numbers = [1, 2, 3];
numbers.forEach((num) => {
num = num * 2; // Doubling the value
});
console.log(numbers); // Output: [1, 2, 3] (unchanged)< /code>
< /div>
< /div>
< /p>
Ожидаемый вывод: < /p>
[2, 4, 6]
Used map() instead (works, but I want to understand forEach).
Tried accessing the array by index:
Код: Выделить всё
numbers.forEach((num, index) => {
numbers[index] = num * 2; // This works, but why doesn’t the first approach?
});
console.log(numbers);
Подробнее здесь: https://stackoverflow.com/questions/795 ... inal-array