2

I have two variables. One is a array and another is a variable which is representing an element from the array.

var arr = ['a', 'b', 'c'];

var elem = arr[1];

I believe elem is a reference of arr[1]; If that is corret, then I am trying to delete that variable from the arr and expecting elem to be undefined something like this...

arr.splice(1,1);

then elem should be undefined.

But somehow this is not happening.

Can anyone please explain me where I understand it wrong or I am doing something wrong? Thanks for your answers.

2
  • only objects has references, primitive values are not. So you just copied element from array Commented Mar 22, 2016 at 5:49
  • Thank you. Please add it to the answers below. I will accept it, Commented Mar 22, 2016 at 5:52

6 Answers 6

1

Only objects has references, primitive values are not. So you just copied element from array

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

2 Comments

Regardless of primitives or object references, as long as value has another reference, it will be retained.
Your answer seems to imply that the result would be different if it was an array of objects. That's not the case though.
1

I believe elem is a reference of arr1;

No it's not. Everything in JavaScript is pass by value. Meaning when the an assignment takes place, e.g.

x = y;

the right hand side (y in this case) is evaluated to a value, and that value is "copied" over to x.

In other words, after the evaluation of the left hand side, there is no knowledge of how the value was created in the first place or where it was stored. That's why changing the array later on has no impact on the copy of the value that was assigned to elem.

Here is some ASCII art:

We start with two containers (arr and elem). arr contains a reference to an array (object). elem is empty.

                                  +----------+              
var arr = ['a', 'b', 'c'];        |0: 'a'    |              
var elem;                         |1: 'b'    |              
                                  |2: 'c'    |              
                                  |length: 3 |              
                                  +----------+              
                                       ^                    
                                       |                    
                                       |                    
                                   |       |         |     |
                                   | #obj1 |         |     |
                                   |       |         |     |
                                   +-------+         +-----+
                                      arr             elem  

When the assignment takes place, the right hand side, arr[0] is evaluated to a value. The result of the evaluation is the string 'a'. That string is put into elem:

                                  +----------+              
elem = arr[0];                    |0: 'a'    |              
                                  |1: 'b'    |              
                                  |2: 'c'    |              
                                  |length: 3 |              
                                  +----------+              
                                       ^                    
                                       |                    
                                       |                    
                                   |       |         |     |
                                   | #obj1 |         | 'a' |
                                   |       |         |     |
                                   +-------+         +-----+
                                      arr              elem 

As you can see, there is no connection between elem and the array.

Later, when the array is spliced, it is mutated to:

                                  +----------+              
arr.splice(1, 1);                 |0: 'b'    |              
                                  |1: 'c'    |              
                                  |length: 2 |                        
                                  +----------+              
                                       ^                    
                                       |                    
                                       |                    
                                   |       |         |     |
                                   | #obj1 |         | 'a' |
                                   |       |         |     |
                                   +-------+         +-----+
                                      arr              elem 

Because the is no connection to elem, it didn't change.

2 Comments

Great explanation. Thank you. But what exactly should I do in this scenario to establish a reference.
@SridharGudimela: JavaScript doesn't support this (as opposed to PHP for example). All you can do is assign / reference the whole array.
0

Splice removes the value from array, it doesn't delete the object itself as long as its value has gone in another reference.

And this is irrespective of the type of value - Primitive or Object reference.

var arr = [{a:'a'}, {a:'b'}, {a:'c'}];
var elem = arr[1];
arr.splice(1,1);

or

var arr = ['a', 'b', 'c'];
var elem = arr[1];
arr.splice(1,1);

In both the cases above, value will be retained inside elem

Comments

0

Every element on the array is a memory reference, now just imagin that a,b,c are pointing To the memeory reference 1,2,3 if you set the var x = a actually you are saying that x also point To tha memory reference 1

If you delete the element a from the array you are just deleting a pointer To the memory not the element itself x is still pointing To the memory, if no one is pointing then the real element will be deleted

That assuming that every element is an object. Only object is referenciable primities are just copy

Comments

0

You are assigning the value, not giving a references.

refer below code, this may give some idea.

var elem=function(pos){return arr[pos];}

 elem(0)  //"a"

 elem(1)  //"b"

 arr.splice(1,1);

 elem(1)  //"c"

Comments

0

In JS, other than undefined, null, boolean, string, and number are object types. They're called primitive types which has fixed sized in memory.

Object types (Reference types) are different. It can be different any of length and do not have any fixed size.

The differences between primitive and object types. Taken from your sample,

var arr = ['a', 'b', 'c']; //Declare and initialize an array.
var elem = arr[1]; //Copy the variable of arr[1] to a new variable
arr.splice(1,1); // Remove the arr[1]
console.log(elem); //Displays arr[1], the copy has not changed.

Now, with the object types,

var arr = ['a', 'b', 'c']; //Declare and initialize an array.
var elem = arr; //Copy the variable of arr to a new variable
arr.splice(1,1); // Remove the arr[1]
console.log(elem); //Displays arr reference. With arr[1] has been removed.

3 Comments

IMO the comparison is a bit misleading since arr and elem point to the same object in the second example. If you where using an array of objects instead and still use arr[1], you'd get the same behavior as in the first example, even though elem contains an object. If you just want to demonstrate the difference between primitives and objects, I would go with a different example.
Unfortunately there isn't really a good way to really demonstrate the difference, since primitives are immutable and objects are not. Put differently: If objects were immutable, you wouldn't know whether the are reference types or value types.
@FelixKling its obvious that arr[1] is a primitive type while arr is an object type.. i make a point how to reference objects in the second sample..

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.