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.
// 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