Skip to content

Chained Shift Method ( ) and Shallow Copy

shift() removes the first element of an array and returns it. Used on a copy of a nested array, it shows the difference between a shallow copy and a deep one, which this page walks through.

const queue = ['Ana', 'Ben', 'Cleo'];
const next = queue.shift();
console.log(next);
console.log(queue);

This prints:

Ana
[ 'Ben', 'Cleo' ]

shift() changes the array it is called on. Its counterpart pop() removes the last element instead.

The spread syntax [...array] builds a new array holding the same elements. When those elements are themselves arrays or objects, the new array holds references to the same inner arrays rather than copies of them:

const tills = [['Ana', 'Ben'], ['Cleo'], ['Dev', 'Eli']];
const copy = [...tills];
console.log(copy === tills);
console.log(copy[0] === tills[0]);

This prints:

false
true

The outer arrays are different arrays, but copy[0] and tills[0] are one and the same inner array. This is what shallow means: only the top level is copied.

Shifting from the outer copy leaves the original alone, because the two outer arrays are separate. Shifting from an inner array changes the original too, because that inner array is shared:

const tills = [['Ana', 'Ben'], ['Cleo'], ['Dev', 'Eli']];
const copy = [...tills];
const firstTill = copy.shift();
console.log(copy);
console.log(tills);
const served = firstTill.shift();
console.log(served);
console.log(tills);

This prints:

[ [ 'Cleo' ], [ 'Dev', 'Eli' ] ]
[ [ 'Ana', 'Ben' ], [ 'Cleo' ], [ 'Dev', 'Eli' ] ]
Ana
[ [ 'Ben' ], [ 'Cleo' ], [ 'Dev', 'Eli' ] ]

After the first shift(), tills still has all three tills. After the second, Ana has gone from tills as well, even though the code never touched tills directly.

Because shift() returns the element it removed, the two steps can be chained. The first shift() returns the first inner array, and the second shift() runs on that inner array:

const tills = [['Ana', 'Ben'], ['Cleo'], ['Dev', 'Eli']];
const copy = [...tills];
const served = copy.shift().shift();
console.log(served);
console.log(copy);
console.log(tills);

This prints:

Ana
[ [ 'Cleo' ], [ 'Dev', 'Eli' ] ]
[ [ 'Ben' ], [ 'Cleo' ], [ 'Dev', 'Eli' ] ]

The copy has lost its first till, and the original has lost its first customer. Chaining Methods explains how chains like this work.

To copy every level, use structuredClone(), which is built into browsers and Node. The copy then shares nothing with the original:

const tills = [['Ana', 'Ben'], ['Cleo'], ['Dev', 'Eli']];
const copy = structuredClone(tills);
copy.shift().shift();
copy[0].push('Fay');
console.log(copy);
console.log(tills);

This prints:

[ [ 'Cleo', 'Fay' ], [ 'Dev', 'Eli' ] ]
[ [ 'Ana', 'Ben' ], [ 'Cleo' ], [ 'Dev', 'Eli' ] ]

structuredClone() cannot copy functions or DOM nodes, and it throws a DataCloneError if it meets one. Deep vs Shallow Copy compares the ways to copy objects.

Steve Griffith covers this topic in his video Spread, Shallow Copies, and Chaining Methods.