Clone JavaScript array using Object.assign() method
You can clone JavaScript arrays using the Object.assign() method. The method is used to copy properties from the source object to the target object by passing an array as a source value and an empty object as a destination value. Below is an example of cloning an array using the Object.assign() method:
Clone JavaScript array using array.reduce() method
You can clone JavaScript arrays using the array.reduce(callback, initialValue) method. The reduce() method executes a callback function for each array element. Below is an example of cloning an array using the array.reduce() method:
Clone JavaScript array using array.forEach() method
You can clone JavaScript arrays using the array.forEach(callback, thisValue) method. This method executes a function for each array element. Below is an example of cloning an array using the array.forEach() method:
Clone JavaScript array using Spread Operator
You can clone JavaScript arrays using the spread ("...") operator. The spread operator ("...") was added to JavaScript in the ES6 standard. JavaScript arrays can be cloned using the following syntax: [...array]. Below is an example of cloning an array using the spread operator:
Clone JavaScript array using for loop
You can also clone JavaScript arrays by using a for loop. Here is an example of cloning an array with the use of a for loop: