0

why does tempArr here give the same value as the one inside the original array

let arr = [[1,2,3,4,5], [6,7,8,9,0]]
let tempArr = arr[0]
arr[0].push(6)
// tempArr.pop()
console.log("arr",arr )
console.log("tempArr",tempArr )

Output:

arr [ [ 1, 2, 3, 4, 5, 6 ], [ 6, 7, 8, 9, 0 ] ]
tempArr [ 1, 2, 3, 4, 5, 6 ]
Joel Green
  • 13
  • 1
  • 3
  • [Is JavaScript a pass-by-reference or pass-by-value language?](https://stackoverflow.com/a/5314911/402037) – Andreas Jul 17 '20 at 09:18
  • Arrays are big (data structure) saved in the memory (contiguous). So, if you assign an array (already assigned) to a variable, the array is not copied but is referenced to the original array memory. See [this](https://www.dyn-web.com/javascript/arrays/value-vs-reference.php#:~:text=JavaScript%20Arrays%3A%20Value%20vs%20Reference&text=This%20happens%20because%20arrays%20are,the%20value%20of%20the%20array). If you want a copy (not a reference) of the original array you **must** specify the operation since copying is a "slow" operation. – Carlo Corradini Jul 17 '20 at 09:20

0 Answers0