Push Multiple Values To An Array In JavaScript

To push multiple values to an array in JavaScript, you can use any of the following methods –

  1. Pass multiple comma separated values to the Array.push() method.
  2. Use the spread operator.
  3. Use the Array.concat() method.
  4. Use Array.splice() method.

Let’s discuss each of the above method in detail below.

Push Multiple Values To An Array In JavaScript

push multiple values to an array in javascript

Passing Multiple Comma Separated Values To The Array.push() Method

This is the most commonly used method to push multiple values to an array in JavaScript. The syntax for this method is as follows –

arrayName.push(value1, value2, ...);

Here, arrayName is the name of the array, and value1, value2, … are the values to be pushed onto the array.

For example –

var arr = [];

arr.push(‘a’, ‘b’, ‘c’);

console.log(arr);

// Output – [‘a’, ‘b’, ‘c’]

In the above code, we have declared an empty array named arr. Next, we have used the Array.push() method to push three string values – ‘a’, ‘b’, and ‘c’ – into the array. Finally, we have logged the contents of the array to the console.

Use The spread Operator

The spread operator is denoted by three dots – …, and is used to spread the contents of an iterable object, such as an array, into individual elements.

The syntax for using the spread operator with arrays is as follows –

arrayName = [...arr];

Here is an example of using the spread operator to push multiple values to an array –

var arr = [‘a’];

arr = [...arr, ‘b’, ‘c’];

console.log(arr);

// Output – [‘a’, ‘b’, ‘c’]

In the above code, we have declared an array named arr with one element – ‘a’. Next, we have used the spread operator to expand the contents of this array and push two more elements – ‘b’ and ‘c’ – into it. Finally, we have logged the contents of the array to the console.

Use The Array.concat() Method

The Array.concat() method is used to concatenate two or more arrays into one array. The syntax for this method is as follows –

arrayName1.concat(arrayName2);

The above syntax concatenates the contents of arrayName2 into arrayName1.

Here is an example of using the Array.concat() method to push multiple values to an array –

var arr1 = [‘a’];

var arr2 = [‘b’, ‘c’];

arr1 = arr1.concat(arr2);

console.log(arr1);

// Output – [‘a’, ‘b’, ‘c’]

In the above code, we have declared two arrays – arr1 and arr2. Next, we have used the Array.concat() method to concatenate the contents of arr2 into arr1. Finally, we have logged the contents of arr1 to the console.

Use Array.splice() Method

The Array.splice() method is used to add new elements to an array at a specified index. The syntax for this method is as follows –

arrayName.splice(index, 0, value1, value2, ...);

The above syntax inserts the values value1, value2, … into arrayName at the specified index.

Here is an example of using the Array.splice() method to push multiple values to an array –

var arr = [‘a’, ‘b’, ‘c’];

arr.splice(arr.length, 0, ‘d’, ‘e’);

console.log(arr);

// Output – [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]

In the above code, we have declared an array named arr with three elements – ‘a’, ‘b’, and ‘c’. Next, we have used the Array.splice() method to insert two new elements – ‘d’ and ‘e’ – into the array at the end. Finally, we have logged the contents of the array to the console.

So there you have it! These are four different ways of pushing multiple values to an array in JavaScript. Try them out and see which one works best for you.

Leave a Reply