2

I have two arrays

arr1 = new Array(); arr2 = new Array(); 

If i do the following:

arr1 = arr2; 

in javascript is this assigning by value or by reference? What i mean is, after doing the above, will further changes in arr2 affect arr1's content and also the other way around?

3
  • 1
    JavaScript is an interpreted language, without pointers... It's irrelevant to ask "byref" or "byval", since it's neither one. However, making two objects equal in general makes them identical, so that the cnahges on the one will reflect the changes on the other. Commented Jan 30, 2012 at 12:37
  • @AndreiBogdan Will you elaborate more your question so I can assist you in your goal? Or is this for learning more only? ;) Commented Jan 30, 2012 at 12:44
  • @H2CO3: I don't think that's correct. It's not at all irrelevant, and JavaScript could have had by value parameter passing or by value assignment semantics (although few languages do), and the answer is that it doesn't. Commented Jan 30, 2012 at 12:45

4 Answers 4

3

Why not create a test case yourself e.g.

arr1 = new Array(); arr2 = new Array(); arr1.push('bob'); arr2.push('joan'); alert(arr1); // Shows "bob" alert(arr2); // Shows "joan" arr1 = arr2; arr2.push("jacob"); arr1.push("goliath"); alert(arr1); // Shows "joan", "jacob", "goliath" alert(arr2); // Also shows "joan", "jacob", "goliath" 

So arr1 refers to arr2 (after assignment of the arr2 to arr1) and contains "joan". Then we push "jacob" and "goliath" but the last alert shows "joan", "jacob" and "goliath" - because Arrays are objects and arr1 and arr2 are pointing to the same Object when the program ends.

Sign up to request clarification or add additional context in comments.

Comments

2

Your case sets arr1 with object/array held by arr2. For that reason, now any change in arr1, like setting an index, will modify the array previously created in arr2.

Comments

2

In javascript arrays are objects. So, in a nutshell, yes, you will be passing a reference.

arr1 = new Array(); arr2 = new Array(); arr1 = arr2; arr1.push('test'); alert(arr2[0]);//test 

if you want to pass it by value, you should make a clone function like:

function cloneValue(value){ if (typeof value != 'object') return value; else { var newObj = {}; for (var prop in value){ newObj[prop] = value[prop]; } return newObj; } } function cloneArray(array){ var newArray = []; for(var i = 0; i < array.length; i++){ newArray[i] = cloneValue(array[i]); } return newArray; } var arr2 = cloneArray(arr1); 

this still has a perk, if the values in the array are not primitive, they are going to be passed by reference again...

I edited the code...

5 Comments

And what is the way to pass it by value then?
@AndréAlçadaPadez But, are you sure this is pass by value? This is a clone and, after all, you're not cloning array indexed contained objects, so, cloned array will have references to same objects as source array :D
yes, that is true, that'w what i say in the last line. but i will get you a solution:
@AndréAlçadaPadez Maybe you've overcomplicated the answer. Just say, "Javascript doesn't support giving arguments by reference but by value by default" :) Everything different than that would be an overkill, it's easier to play with that limitation than introducing unexisting language-level features, what do you think?
I think you're right, my initial answer was just that. He asked me fOr solution, i gave it
0

Here is a clone function I wrote...

 /** * Clone an Object or Array. * * @param {Object} * obj the Object or Array that should be cloned * @param {Boolean} * deep if true also clones the elements of an Object or Array, * default is true * @return * @type Object */ function cloneObject(obj, deep) { try { deep = !deep ? false : true; if (obj.constructor === Object) { var nObj = {}, elem = null; for (elem in obj) { if (obj.hasOwnProperty(elem)) { nObj[elem] = deep ? cloneObject(obj[elem]) : obj[elem]; } } return nObj; } if (obj.constructor === Array) { var nArr = [], i = 0; for (i = 0; i < obj.length; i++) { nArr[i] = deep ? cloneObject(obj[i]) : obj[i]; } return nArr; } } catch (e) { console.error("cloneObject(", arguments, ") >", e); } return obj; }; 

3 Comments

How does this answer the question at all O_O
It only shows how to clone an array or object to be able to pass a reference by value. Nothing more, nothing less...
@Esailija Life is a mistery, isn't it?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.