This post will discuss how to create a shallow and deep copy of an array in JavaScript.

With a shallow copy, the values are copied by reference when the array is nested. This means that if the referenced object is changed, the changes are reflected in both the original array and its copy. There are several ways to create a shallow and deep copy of an array in JavaScript. All the functions below copies any nested objects or arrays by reference except the JSON functions, jQuery’s $.extend() function with the first argument set to true, and the Lodash’s _.cloneDeep() function.

1. Using Array.slice() function

The slice() is commonly used built-in function that returns a shallow copy of a portion of an array into a new array object. We can use this function to copy the whole array by omitting the start and end arguments, or specify a range of elements to copy.

Download  Run Code

2. Using Spread operator

The spread operator (…) is a feature of ES6 syntax that allows us to expand an iterable object into individual elements. We can use this operator inside an array literal ([]) to create a new array with the same elements as the original array, while offering the best performance.

Download  Run Code

 
Instead of using the array literal, we can use the spread operator with the Array.of() function, which creates a new Array instance from the expanded sequence of elements.

Download  Run Code

3. Using Array.from() function

The Array.from() is a built-in function that allows us to create a new array from an array-like or iterable object. We can use this function to copy an array by passing it as the first argument. We can also optionally provide a map() function as the second argument to modify the elements of the new array.

Download  Run Code

4. Using Array.concat() function

The Array.concat() is a built-in function that returns a new array that is the result of merging two or more arrays. We can use this function to copy an array by passing an empty array as the first argument and the original array as the second argument.

Download  Run Code

 
We can also use the concat() function in another way. If no parameter is specified, the concat() function returns a shallow copy of the calling array.

Download  Run Code

5. Using Object.assign() or Object.values() function

The Object.assign() is a built-in function that copies the values of all enumerable own properties from one or more source objects to a target object. We can use this function to copy an array by passing an empty array as the target object and the original array as the source object.

Download  Run Code

 
The Object.values() is a built-in function that returns an array of the values of the enumerable own properties of an object. We can use this function to extract the values of the array object.

Download  Run Code

6. Using Array.map() function

The Array.map() is a built-in function that creates a new array with the results of calling a callback function on every element in the original array. We can use this function to copy the elements of an array by passing an identity function as a callback.

Download  Run Code

7. Using JSON

The JSON.parse() and JSON.stringify() are the built-in functions that convert an object or an array to a JSON string and vice versa. We can use these functions to create a deep copy of an array by first converting it to a JSON string and then parsing it back to an array. This works with any type of value, but it will lose any functions or symbols in the array:

Download  Run Code

8. Using jQuery

The $.extend() function from the jQuery library copies the values of all enumerable own properties from one or more source objects to a target object. We can use this function to copy an array by passing an empty array as the target object and the original array as the source object. We can also pass a true value as the first argument to perform a deep copy of nested arrays or objects.

Download Code

9. Using Lodash/Underscore Library

With Lodash library, we can use the _.extend() or _.assign() or _.clone() function to get a shallow copy of an array. These functions work with any type of value, and they can preserve functions and symbols in the array. To duplicate any nested objects or arrays, we should use the _.cloneDeep function instead, which returns a deep copy.

Download Code

 
The underscore library also offers the _.extend(), and _.clone() function, to create a shallow-copy of the specified object. However, the underscore library currently does not have any function that can perform a deep copy operation on an array.

Download Code

That’s all about creating a shallow and deep copy of an array in JavaScript.