3 Practical uses of array slice method in JavaScript | Interview Guide

preview_player
Показать описание
The slice( ) method copies a given part of an array and returns that copied part as a new array. It doesn’t change the original array.

The start argument is a 0-based index identifying where to begin extraction,
while the end argument is a 0-based index identifying where to end.
Both start and end parameters are optional.

In this tutorial, we will show you 3 practical uses of the JavaScript array slice() method.
1. To clone an array
2. Copy portion of an array
3. Convert array like objects into arrays

To Clone an array
Because arrays in JS are reference values, so when you try to copy it using the = it will only copy the reference to the original array and not the value of the array. To create a real copy of an array, you need to copy over the value of the array under a new value variable. That way this new array does not reference to the old array address in memory.

A shallow copy refers to the fact that only one level is copied, and that will work fine for an array or object containing only primitive values.
Copy a portion of array.

Some times it might be scenario to extract some part of array without effecting original array, then slice method be approach
Рекомендации по теме
visit shbcf.ru