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.
arr.slice()
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Yes! slice will create a copy of the existing array when assigned to a new value, the original array is not bothered. Example:
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.