Sometimes, you need to increase the values in your array by a specific value. This article will share some solutions to help increment the values in an array using JavaScript. Let’s get started.
How to increment the values in an array using JavaScript
The solutions given to increment the values in an array using JavaScript are:
- Use the
for
loop - Use the
map()
method - Use the
Array.from()
method
Here are the details of each solution with specific examples.
Use the for loop
We need to iterate over all the array elements using the for
loop. At each loop, we increment that element by a specific value.
Example:
const numArray = [1, 2, 3, 4]; // Increment the values in the array by 3 for (var i = 0; i < numArray.length; i++) { numArray[i] += 3; } console.log(numArray);
Output:
[ 4, 5, 6, 7 ]
In this example, we used the for
loop to increment the values in the array by 3.
Use the map() method
The map()
method creates a new array by calling a function on every element in the calling array.
Syntax:
array.map(function(element, index, array), thisArg)
Parameters:
function(): function that is called for every array element.
element: the value of the current element.
index: the index of the current element.
array: the array map has been called.
thisArg: a value to use for the function.
In the following example, we describe how to use the map()
method to increment the value in an array by 2.
Example:
const numArray = [1, 2, 3, 4]; // Increment the values in the array by 2 const newArr = numArray.map((x) => x + 2); console.log(newArr);
Output:
[ 3, 4, 5, 6 ]
The map()
method returns a new array and does not change the original array. However, if you want to change the original array directly, you can use the forEach()
method instead.
Use the Array.from() method
You can use the Array.from()
method. This function returns an array from any input value with a length property.
Syntax:
Array.from(object, function, thisValue)
Parameters:
object: the object to convert to an array.
function: a callback function.
thisValue: a value to use for the function
The callback function is a mapping function used to add a specific value to each element in the array.
In the following example, we describe how to use the Array.from()
method to increment the value in an array by 4.
Example:
const numArray = [1, 2, 3, 4]; // Increment the values in the array by 4 const newArr = Array.from(numArray, (x) => x + 4); console.log(newArr);
Output:
[ 5, 6, 7, 8 ]
Summary
This article shared some JavaScript solutions to increment the values in an array. If you want to change the original array directly, use the for
loop or the forEach()
method. If you don’t want to change the original array, use the Array.from()
or map()
methods. Thanks for reading.
Maybe you are interested:
- Count the True values in an Array using JavaScript
- Push multiple Values to an Array in JavaScript
- Check if Multiple Values exist in Array in JavaScript

Hello, my name’s Bruce Warren. You can call me Bruce. I’m interested in programming languages, so I am here to share my knowledge of programming languages with you, especially knowledge of C, C++, Java, JS, PHP.
Name of the university: KMA
Major: ATTT
Programming Languages: C, C++, Java, JS, PHP