Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

How to clone array in js

// How to clone array

const log = console.log;
const arr1 = ["Fahad", "Shaikh", "testing"]; // Made array and assigned some value init

// Method 1
// const arr2 = arr1.slice(0); // First method to clone the array in another array
// log(arr1);

// Method 2
// const arr2 = [].concat(arr1);
// log(arr2);

// Method 3 and newest method
// This is called spread operator in js

// const arr2 = [...arr1];
// log(arr2);
 
PREVIOUS NEXT
Tagged: #How #clone #array #js
ADD COMMENT
Topic
Name
5+6 =