# 1.1.1.Pass array by value

* 在js中, 將array傳入function中做運算若沒有經過處理將會以pass by reference傳入, 因此當在function中的array內容被改變, 將會影響原本的array
* 為了避免此情況, 可以將array以pass by value的方式傳入, 以避免污染原有的array
* pass by reference v.s. pass by value
  * native array method - "slice()" will return a shallow copy of the array.

![](https://3195929128-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4M0G8GO2MkGtHfarAf%2F-M4M0IHoyFvfGKjf7P-T%2F-M4M0MCMEL6Ax7EKUv--%2F%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7%202017-03-26%20%E4%B8%8B%E5%8D%881.01.12.png?generation=1586302920368078\&alt=media)

* [https://codepen.io/JenHsuan/pen/aJjVov?editors=0012](https://codepen.io/JenHsuan/pen/aJjVov?editors=0012)
* [reference](http://orizens.com/wp/topics/javascript-arrays-passing-by-reference-or-by-value/)
* [https://codepen.io/JenHsuan/pen/aJjVov?editors=0012](https://codepen.io/JenHsuan/pen/aJjVov?editors=0012)
