Open In App

How to Add a Number to Every Item in an Array?

Last Updated : 19 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Adding a number to every item in an array is a common operation in JavaScript, often required for tasks like incrementing values or performing mathematical operations on array elements.

Below are the approaches to add a number to every item in an array:

Using Array.map() Method

The map() method creates a new array by applying a provided function to each element of the original array.

Syntax:

const newArray = originalArray.map(item => item + numberToAdd);

Example: To demonstrate adding a number to every element in an array using the JavaScript built-in method array.map() method.

JavaScript
const GFG = [1, 2, 3, 4, 5];
const numberToAdd = 10;
const newArray = GFG
    .map(item => item + numberToAdd);
console.log(newArray);

Output
[ 11, 12, 13, 14, 15 ]

Using a for Loop

We can iterate through the array using a for loop and add the number to the each element individually. the for loop iterates through each element and updates it.

Syntax:

for (let i = 0; i < originalArray.length; i++)
{
originalArray[i] += numberToAdd;
}

Example: To demonstrate adding a number to every item in an array using the for loop in JavaScript.

JavaScript
const originalArrays = [1, 2, 3, 4, 5];
const numberToAdd = 10;
for (let i = 0; i < originalArrays.length; i++) {
    originalArrays[i] += numberToAdd;
}
console.log(originalArrays);

Output
[ 11, 12, 13, 14, 15 ]

Using reduce Method

The reduce() method executes a reducer function on each element of the array.

Syntax:

newArray = array.reduce((accumulator, currentValue) => accumulator.push(currentValue + numberToAdd)

Example: To demonstrate adding a number to every item in an array using the reduce method in JavaScript.

JavaScript
let array = [1, 2, 3, 4];
let numberToAdd = 5; 
let newArray = array.reduce((accumulator, currentValue) => {
  accumulator.push(currentValue + numberToAdd);
  return accumulator;
}, []);

console.log(newArray);

Output
[ 6, 7, 8, 9 ]

Using forEach() Method

The forEach() method executes a provided function once for each array element. This approach is useful for performing operations on each element of the array without returning a new array.

Syntax

originalArray.forEach((item, index, arr) => {
arr[index] = item + numberToAdd;
});

Example: To demonstrate adding a number to every element in an array using the JavaScript built-in forEach() method.

JavaScript
// Original array
const originalArray = [1, 2, 3, 4, 5];
const numberToAdd = 2;

// Using forEach to add number to each element
originalArray.forEach((item, index, arr) => {
    arr[index] = item + numberToAdd;
});

// Output the modified array
console.log('Modified array:', originalArray);

Output
Modified array: [ 3, 4, 5, 6, 7 ]

Using the Array.from() Method

The Array.from() method creates a new array instance from an array-like or iterable object. By providing a mapping function as the second argument, we can add a number to every item in the array.

Example: The below example demonstrates how to add a number to every item in an array using the Array.from() method in JavaScript.

JavaScript
// Original array
const originalArray = [1, 2, 3, 4, 5];
// Number to add
const numberToAdd = 3;

// Using Array.from() method to add a number to every item in the array
const newArray = Array.from(originalArray, item => item + numberToAdd);

console.log(newArray);

Output
[ 4, 5, 6, 7, 8 ]

Next Article

Similar Reads