DEV Community

loading...

How to copy one array to another in javascript?

heysujal profile image Sujal Gupta ・1 min read

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);

Discussion (0)

pic
Editor guide