Back to JavaScript
2026-04-165 min read

Spread Operator to Clone an Array (JavaScript)

Learn Spread Operator to Clone an Array (JavaScript) step by step with clear examples and exercises.

Title: Mastering the Spread Operator for Array Cloning in JavaScript


Why This Matters

In JavaScript, arrays are objects that share memory locations. Modifying an array can unintentionally change another array that shares the same reference. The spread operator (...) is a powerful ES6 feature that allows us to create a new array by copying all elements from an existing one, thus avoiding shared references and potential unintended consequences.


Prerequisites

Before diving into the spread operator, you should be familiar with:

  • Basic JavaScript syntax and data types (numbers, strings, booleans, null, undefined)
  • Variables and assignment operators
  • Arrays and array methods like push(), pop(), shift(), unshift()
  • Objects and object properties
  • Template literals
  • Basic understanding of functions and control structures such as loops and conditionals
  • Understanding of ES6 features, including arrow functions, template literals, and destructuring assignment

Core Concept

The spread operator is a three-dot notation (...) that allows you to expand an iterable object such as an array or string into individual elements. In the context of arrays, it can be used to clone an array by creating a new one with all the existing elements. This prevents altering the original array when making modifications to the cloned one.

Syntax

Here's the basic syntax for using the spread operator to clone an array:

const originalArray = [1, 2, 3];
const clonedArray = [...originalArray];

In this example, clonedArray is a new array that contains all elements from originalArray.

Using the Spread Operator with Destructuring Assignment

You can also use the spread operator in combination with destructuring assignment to clone an array and assign its elements to variables simultaneously:

const originalArray = [1, 2, 3];
const [first, second, third] = [...originalArray];
console.log(first); // Output: 1
console.log(second); // Output: 2
console.log(third); // Output: 3

Spreading Arrays into Other Arrays

The spread operator can also be used to merge arrays by combining them into a single array:

const array1 = [1, 2];
const array2 = [3, 4];
const mergedArray = [...array1, ...array2];
console.log(mergedArray); // Output: [ 1, 2, 3, 4 ]

Spreading Arrays into Function Arguments

The spread operator can be used to pass an array as individual arguments to a function:

function sum(a, b, c) {
return a + b + c;
}

const originalArray = [1, 2, 3];
console.log(sum(...originalArray)); // Output: 6

Worked Example

Let's look at into a practical example to better understand the spread operator and how it can help us avoid unintended consequences when working with arrays.

const originalArray = [1, 2, 3];
const clonedArray = [...originalArray];

// Modifying the cloned array
clonedArray[0] = 'a';
console.log(clonedArray); // Output: [ 'a', 2, 3 ]
console.log(originalArray); // Output: [ 1, 2, 3 ]

In this example, we first create an original array [1, 2, 3]. Then, we clone the array using the spread operator and store the result in a new variable called clonedArray. When we modify the first element of the cloned array (changing it from 1 to 'a'), the original array remains unchanged.

Deep Cloning Arrays with Nested Objects

When dealing with arrays containing nested objects, you may want to use a library like lodash or recursively clone the arrays:

function deepClone(array) {
return [].concat(...array.map(item => Array.isArray(item) ? deepClone(item) : item));
}

const originalArray = [1, 2, { a: 3 }, 4];
const clonedArray = deepClone(originalArray);
console.log(clonedArray); // Output: [ 1, 2, { a: 3 }, 4 ]

Common Mistakes

Forgetting to use the spread operator when creating a copy

When you forget to use the spread operator, you end up with two references pointing to the same array:

const originalArray = [1, 2, 3];
const clonedArray = originalArray;

// Modifying the cloned array affects the original array
clonedArray[0] = 'a';
console.log(clonedArray); // Output: [ 'a', 2, 3 ]
console.log(originalArray); // Output: [ 'a', 2, 3 ]

Using the spread operator incorrectly with non-array objects

The spread operator only works with iterable objects like arrays and strings. When used with other types of objects (e.g., plain objects), it will result in a syntax error:

const originalObject = { a: 1, b: 2 };
const clonedObject = ...originalObject; // SyntaxError: Unexpected token '...'

Not understanding the difference between ... and Array.from()

Both the spread operator and Array.from() can be used to create new arrays from existing ones, but they have subtle differences in their behavior. For example, when cloning an array containing nested arrays, the spread operator will only copy the top-level arrays, while Array.from() will recursively copy all levels:

const originalArray = [1, [2, 3], 4];

// Using the spread operator
const clonedArray1 = [...originalArray];
console.log(clonedArray1); // Output: [ 1, [ 2, 3 ], 4 ]

// Using Array.from()
const clonedArray2 = Array.from(originalArray);
console.log(clonedArray2); // Output: [ 1, [ 2, 3 ], 4 ]

Practice Questions

  1. Write a function that takes an array and returns a new array with all its elements doubled using the spread operator.
function doubleArray(arr) {
const doubled = [...arr].map(num => num * 2);
return doubled;
}

const originalArray = [1, 2, 3];
console.log(doubleArray(originalArray)); // Output: [ 2, 4, 6 ]
  1. Given the following array, use the spread operator to create a new array that contains only odd numbers.
const originalArray = [1, 2, 3, 4, 5, 6];
const oddNumbers = [...originalArray].filter(num => num % 2 !== 0);
console.log(oddNumbers); // Output: [ 1, 3, 5 ]
  1. Write a function that takes an array and returns a new array with all its elements sorted in ascending order using the spread operator and sort().
function sortArray(arr) {
const sorted = [...arr].sort((a, b) => a - b);
return sorted;
}

const originalArray = [5, 2, 8, 1, 6];
console.log(sortArray(originalArray)); // Output: [ 1, 2, 5, 6, 8 ]

FAQ

Q1: Why can't I use the spread operator to clone an object?

A1: The spread operator works with iterable objects like arrays and strings because they have a defined order of properties or elements. Plain objects do not have an inherent order, so using the spread operator on them would result in an unpredictable order of properties or a syntax error.

Q2: What happens if I use the spread operator with an empty array?

A2: Using the spread operator with an empty array will return an empty array. This can be useful when you need to create a new, empty array and don't want to use [].

const newArray = [...]; // Output: []

Q3: Can I use the spread operator in ES5 syntax?

A3: The spread operator is an ES6 feature, so it won't work in older versions of JavaScript. However, you can transpile your code using tools like Babel to make it compatible with older browsers.

Q4: Is there a performance difference between the spread operator and Array.from()?

A4: In most cases, both the spread operator and Array.from() have similar performance when cloning arrays. However, for large arrays or arrays containing many nested arrays, using Array.from() with the map() method may be more efficient as it allows you to perform additional operations on each element during the cloning process.

Q5: Can I use the spread operator in combination with destructuring assignment?

A5: Yes! The spread operator and destructuring assignment can be used together to clone an array and assign its elements to variables simultaneously.

const originalArray = [1, 2, 3];
const [first, second, third] = [...originalArray];
console.log(first); // Output: 1
console.log(second); // Output: 2
console.log(third); // Output: 3
Spread Operator to Clone an Array (JavaScript) | JavaScript | XQA Learn