loading...

re: A Comprehensive Guide to JavaScript - Part 4 - Arrays VIEW POST

TOP OF THREAD FULL DISCUSSION
re: I thought 'slice' method will create a copy of the existing array. Enlighten me here.
 

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"]

 

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

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

code of conduct - report abuse