How to use Array.slice() Method In Javascript
We use the slice() method to create a copy of an array by its value in JavaScript. This method creates a new array with a subset of the elements from the original array.
Example:
const originalArr = [1, 2, 3, 4, 5, 6];
const clonedArr = originalArr.slice();
console.log(clonedArr);
Output
[ 1, 2, 3, 4, 5, 6 ]
How to Copy Array by Value in JavaScript ?
In this article, we will see how to create a copy of an array by value using JavaScript. Javascript has various ways to clone the array.
We can use some methods to create a copy of an array that is given below:
Table of Content
- Using Spread Operator
- Using Array.from() Method
- Method 3: Using Array.slice() Method
- Using structuredClone() Method
- Using the map()