Why is the original array modify if i made a copy using the spread operator?

function fruitInventory(arr) {

let arrCopy = […arr]

arrCopy[0][1] -= 1;

console.log(arr)

console.log(arrCopy)

}
(fruitInventory([ [“APPLE”, 6] , [“ORANGE”, 2]]));

Result:

[ [ ‘APPLE’, 5 ], [ ‘ORANGE’, 2 ] ]
[ [ ‘APPLE’, 5 ], [ ‘ORANGE’, 2 ] ]

Hi @Petar,

The spread operator creates a shallow copy of an array.

Link below explains it in detail:

1 Like

Thank you very much @manjupillai16 , very clear.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.