DEV Community

Sujal Gupta
Sujal Gupta

Posted on • Updated on

How to copy one array to another in javascript?

const name1 = ['Sujal','Nitin','Aman','Harshit'];
const name2 = name1;
// this does not copy rather it gives a reference meaning that any change made in either of the array would also reflect in other as well. 
const name3 = name1.slice();
const name4 = [].concat(name1);
const name5 = [...name1];
const name6 = Array.from(name1);
Enter fullscreen mode Exit fullscreen mode

Top comments (0)