DEV Community

Discussion on: A Comprehensive Guide to JavaScript - Part 4 - Arrays

Collapse
 
kgprajwal profile image
Prajwal

Yes! slice will create a copy of the existing array when assigned to a new value, the original array is not bothered. Example:

var arr = ["school", "college", "restaurant", "hospital"];
var newarr = arr.slice(0, 2);
console.log(arr);
console.log(newarr);

Output:
["school", "college", "restaurant", "hospital"]
["school", "college"]

Collapse
 
ericbabi12 profile image
E. Babi

Great. So the conclusion is that if you dont specify the arguments (0,2), arr.slice() will reproduce the original array right?

Thread Thread
 
kgprajwal profile image
Prajwal

Yes! Just arr.slice() will reproduce the original array.