Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

how to copy a javascript array

let arr = ["jason", "james", "jelani"];
let arrCopy = arr.slice();
//Note that this is "slice" and not "splice". 
//arr.splice() would be very different!

console.log(arrCopy);//["jason", "james", "jelani"]

//Makes deep copy
arrCopy[0] = "jamil";
console.log(arr); //["jason", "james", "jelani"]
console.log(arrCopy);//["jamil", "james", "jelani"]
Source by www.samanthaming.com #
 
PREVIOUS NEXT
Tagged: #copy #javascript #array
ADD COMMENT
Topic
Name
8+1 =