Javascript spread operator

Javascript spread operator allows an iterable to expand in places where 0+ arguments are expected. It is mostly used in the variable array where there is more than 1 value is expected. It allows us the privilege to obtain a list of parameters from an array.

Syntax:

let variablename1 = [...value];

Example: This example shows the implementation of above-explained appraoch.

Javascript
// Input arrays
let Array1 = [2, 4, 6, 8];
let Array2 = [3, 5, 7]

// Combine and create new array 
// using spread operator
newArray = [ ...Array1, ...Array2]

// Display output
console.log(newArray);

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

How to append an element in an array in JavaScript ?

Appending an element to an array in JavaScript means adding a new item to the end of the array. , this process extends the array’s length, accommodating new data. JavaScript offers several methods for appending elements which accepts one or more elements as arguments and adds them to the end of the array.

These are the following ways to solve the problem:

Table of Content

  • Using JavaScript push() Method
  • Using JavaScript unshift() Method
  • Using JavaScript splice() Method
  • Using JavaScript concat() Method
  • Javascript spread operator
  • Using Lodash _.concat() function
  • Using the array length property

Similar Reads

1. Using JavaScript push() Method

JavaScript push() Method will add an element to the end of an array, while its twin function, the pop() method, will remove an element from the end of the array. If you need to add an element or multiple elements to the end of an array, the push() method will almost always be your simplest and quickest option....

2. Using JavaScript unshift() Method

JavaScript unshift() Method will add an element to the beginning of an array, while its twin function, shift(), will remove one element from the beginning of the array....

3. Using JavaScript splice() Method

JavaScript splice() Method modifies the content of an array by removing existing elements and/or adding new elements....

4. Using JavaScript concat() Method

JavaScript concat() Method returns a new combined array comprised of the array on which it is called, joined with the array (or arrays) from its argument. This method is used to join two or more arrays and this method does not change the existing arrays, but returns a new array, containing the values of the joined arrays....

5. Javascript spread operator

Javascript spread operator allows an iterable to expand in places where 0+ arguments are expected. It is mostly used in the variable array where there is more than 1 value is expected. It allows us the privilege to obtain a list of parameters from an array....

6. Using Lodash _.concat() function

More than 1 value is expected. It allows us to merge values into the array....

7. Using the array length property

JavaScript arrays have a length property that can be used to append elements to the end of an array dynamically. By assigning a value to an index that is equal to the current length of the array, a new element can be appended....