Last updated: Mar 4, 2024
Reading timeยท3 min
forEach()
for
loopTo increment the values in an array:
Array.map()
method to iterate over the array.map()
method will return a new array containing the incremented values.const arr = [1, 2, 3]; const newArr = arr.map(num => num + 1); console.log(newArr); // ๐๏ธ [2, 3, 4]
The function we passed to the Array.map() method gets called with each element in the array.
On each iteration, we add 1
to the current number and return the result.
The map()
method returns a new array containing the values returned from the
callback function.
Array.map()
method doesn't mutate the original array, it returns a new array.If you need to create an incrementing array, use the Array.from()
method.
const arr1 = Array.from(Array(5), (_, index) => index); console.log(arr1); // ๐๏ธ [ 0, 1, 2, 3, 4 ] const arr2 = Array.from(Array(5), (_, index) => index + 1); console.log(arr2); // ๐๏ธ [ 1, 2, 3, 4, 5 ]
The function we passed to the Array.from()
method is a map()
function, just
like in the previous example.
You can use the index to create an incrementing array.
If you need to increment a specific value, access the array at an index and increment the value.
const arr = [1, 2, 3]; arr[0] += 1; console.log(arr[0]); // ๐๏ธ 2 arr[0] = arr[0] + 1; console.log(arr[0]); // ๐๏ธ 3
The
addition assignment (+=)
operator is a shorthand for myVariable = myVariable + value
.
0
, and the last element has an index of array.length - 1
.When you access the array at the specific index and assign a new value to it, you mutate the original array and change its value in place.
Note that when using the addition assignment (+=) operator with primitives like
strings or numbers, the variable has to be declared using the let
keyword.
let a = 1; a += 5; console.log(a); // ๐๏ธ 6 // ๐๏ธ same as above a = a + 5; console.log(a) // ๐๏ธ 11 const b = 1; // โ๏ธ SyntaxError b ++ 5;
When using the addition assignment (+=) operator with primitives, we reassign the variable.
This is not the case with arrays or objects, where we change the value of a specific element without reassigning the actual variable.
Alternatively, you can use the Array.forEach() method.
forEach()
This is a two-step process:
Array.forEach()
method to iterate over the array.1
.const arr = [1, 2, 3]; arr.forEach((num, index) => { arr[index] = num + 1; }); console.log(arr); // ๐๏ธ [2, 3, 4]
The function we passed to the Array.forEach() method gets called with each element in the array.
We access the element at the current index and increment its value by 1
.
The forEach()
method returns undefined
, so we have to perform some kind of
mutation to persist the state.
You can also use a basic for
loop.
for
loopThis is a two-step process:
for
loop to iterate over the array.1
.const arr = [1, 2, 3]; for (let index = 0; index < arr.length; index++) { arr[index] = arr[index] + 1; } console.log(arr); // ๐๏ธ [ 2, 3, 4 ]
The code sample is very similar to the previous one.
However, instead of using the Array.forEach()
method, we used a basic for
loop to iterate over the array.
You can learn more about the related topics by checking out the following tutorials: