Modify all Array Elements in JavaScript-Code Examples

Radu TM

Modify all Array Elements in JavaScript

JavaScript by Radu TM β€’ June 17, 2022

// Modify all Array Elements in JavaScript
var arr = [1, 2, 3, 4, 5];

for (var i = 0; i < arr.length; i++) {
  arr[i] = arr[i] + 1;
}

console.log(arr); // [2, 3, 4, 5, 6]

0

17.748

Modify all Array Elements in JavaScript

JavaScript by Radu TM β€’ June 17, 2022

array.forEach(function(element) {
  // do something with element
});

0

17.748

Modify all Array Elements in JavaScript

JavaScript by Radu TM β€’ June 17, 2022

const fruits = [
  "🍎", "🍊", "πŸ‹", "🍌", "πŸ‰", "πŸ‡", "πŸ“", "πŸ’", "πŸ‘", "🍈"
];

// Modify each element in the array
for (let i = 0; i < fruits.length; i++) {
  // add an apple to each fruit
  fruits[i] = fruits[i] + "🍏";
}

console.log(fruits);

0

17.748

Related JavaScript Queries

Latest JavaScript Queries

Explore code examples by categories