How do I pass the value instead of the reference of an array?

I think you can use this to copy the value instead of the reference:

var b = a.slice(0);  

EDIT
As the comments have mentioned and it’s also mentioned here: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/slice

slice does not alter the original array, but returns a new “one level deep” copy that contains copies of the elements sliced from the original array. Elements of the original array are copied into the new array as follows:

  • For object references (and not the actual object), slice copies
    object references into the new array. Both the original and new array
    refer to the same object. If a referenced object changes, the changes
    are visible to both the new and original arrays.

  • For strings and numbers (not String and Number objects), slice copies
    strings and numbers into the new array. Changes to the string or
    number in one array does not affect the other array.

If a new element is added to either array, the other array is not affected.

Leave a Comment