5

I realise JavaScript has no pointers, however I noticed this "pointer" behaviour when looping through arrays that contains objects, but not the similar behaviour when an array contains numbers (for instance).

var ARR_num = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
for (var i = 0, len = ARR_num.length; i < len; i++) {
  var item = ARR_num[i];
  item++;
}
console.log(ARR_num);
//Outputs [0,1,2,3,4,5,6,7,8,9]

Now with an array with objects

var ARR_obj = [{}, {}, {}];
for (var i = 0, len = ARR_obj.length; i < len; i++) {
  var item = ARR_obj[i];
  item.pointer = true;
}
console.log(ARR_obj);
//Outputs [{pointer: true}, {pointer: true}, {pointer: true}]

Why these two distinct behaviours?

6
  • because item === ARR_obj[i]. item refers to ARR_obj[i] Commented Aug 21, 2017 at 9:53
  • 1
    it's because javascript passes integer as value but objects as reference (actually the reference's value to be precise). It means that when you store it in item, the change made on item affects the original object but not the original integer Commented Aug 21, 2017 at 9:54
  • 1
    Possible duplicate of Modifying a copy of a JavaScript object is causing the original object to change Commented Aug 21, 2017 at 9:59
  • 1
    the for loops do not behave differently in your examples - it's what you're doing to the data in the array that is different Commented Aug 21, 2017 at 10:21
  • @Rajesh although I can understand that, my question can be a duplicate, the pointed question doesn't fully answer the primitives part. Commented Aug 22, 2017 at 11:03

4 Answers 4

1

Why these two distinct behaviors?

when you assign an object to another variable, the new variable points to same objects hence when you change new variables properties, the Objects gets mutated example:

var a= {name: "some"};
var b = a;
b.name = "newName";
console.log(a.name);// "newName"

when you assign a primitive type to another variable, its just a new variable and has no reference to old variable, hence changing new variable won't affect old one. example:

var a = "some";
var b = a;
b = "newName";
console.log(a);//"some"

hope this will help!!

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

Comments

0

{} is the object literal syntax, which creates (in your case) new objects in an array. When looping through it and using ARR_obj[i], you get the referenced object, which is then manipulated.

Or to put this in other (more general) words:

Primitives (undefined, null, boolean, string and number) are passed by value, while Objects are passed by references (or to be precise by a copy of the reference).

Comments

0

Why these two distinct behaviours?

Because you have two distinct operations. In the latter, you're mutating object's state

item.pointer = true;

item keeps pointing at the same object. Only the internals of the object are changed.

But here

item++; # equivalent to item = item + 1

you reassign item. It will point to another integer after the operation. If you do a similar operations in the object loop

item = { pointer: true }

Then your original array elements won't be affected, and you won't observe any "pointer semantics".

And yes, it also has something to do with integers being primitive/immediate values.

Comments

0

As we know, All values (that are not primitives) are object pointers. So item is an object pointer in use case 2 where as in use case 1 its integer.

In use case 2, its referring to value "true" for all iteration . so as per code written its giving currect result.

in general JavaScript does not support passing parameters by reference, so using Objects in Javascript , we can pass a reference.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.