'what is the use of adding triple dot in array in javascript?

What is the use of adding triple dots in an array in javascript?

my code sample is:

let array = [newArray]

and

let array = [...newArray]*


Solution 1:[1]

Adding triple dots on an array is called spread syntax

By using it like this newArray = [...existingArray], we spread the existingArray into newArray so newArray has all the items of existingArray now.

Common use case of this syntax is to add new items into a new array with the existing items from another array

const oldArr = [2,3,4];
const newArr = [1,...oldArr]; //newArr = [1,2,3,4]

//If you put it like this..
const oldArr = [2,3,4];
const newArr = [oldArr]; //newArr = [[2,3,4]]

See this for more info : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1