3

I am using ArrayList as my array,

let ArrayList   =  ['a','b','c','d','e','f'];

I am confused in between Method 1 and Method 2 because in both case I Referenced ArrayList by another you can also check logs over this link https://jsfiddle.net/mnjsnj/u1fms8wx/2/

Method 1

let Method1 = ArrayList;  // Referenced arrayList by another variable 
ArrayList= []; // Empty the array 
console.log(Method1); // Output ['a','b','c','d','e','f']

Method 2

let Method2 = ArrayList;  // Referenced arrayList by another variable 
ArrayList.length = 0; // Empty the array by setting length to 0
console.log(Method2 ); // Output []
Manoj Bhardwaj
  • 736
  • 1
  • 9
  • 25
  • Initially, both variables reference the same array. Then in the first example a new array is assigned to *ArrayList*, *Method1* is not affected. In the second example, the length of the array that both variables reference is changed. – RobG Feb 04 '19 at 06:10
  • Thank you RobG answer is clear now for me – Manoj Bhardwaj Feb 04 '19 at 06:18

2 Answers2

1

ArrayList is being emptied after first method , so you are assigning an empty array to Method2

let ArrayList = ['a', 'b', 'c', 'd', 'e', 'f'];

let Method1 = ArrayList; // Referenced arrayList by another variable 
ArrayList = []; // Empty the array 
console.log(Method1); // Output ['a','b','c','d','e','f']

console.log('ArrayList after Method1....!' , ArrayList)
// here an empty array is assinged to Method2
let Method2 = ArrayList; // Referenced arrayList by another variable 
ArrayList.length = 0; // Empty the array by setting length to 0
console.log(Method2); // Output []
brk
  • 48,835
  • 10
  • 56
  • 78
1

The trick to understanding what is going on here is to understand how variables work in JavaScript and what the assignment (=) operator does.

A variable is just a bound name to a memory location.

When we assign something to a variable via the = operator, we only change what that variable is pointing to, we are not changing the actual data at the existing memory location, we're just making the variable not point to it anymore.

// Method1 now points to the same memory location as ArrayList
let Method1 = ArrayList;
// ArrayList variable now points to a new memory location containing an empty array
ArrayList = []; 
// Method1 is still pointing to the original memory location so the array is unaffected
console.log(Method1);

In the second example, you are directly affecting the data at the memory location that ArrayList is pointing to by changing its length to 0.

// point Method2 variable to the same location as ArrayList variable
let Method2 = ArrayList; 
// change the data stored at the location ArrayList is pointing to
ArrayList.length = 0; // Empty the array by setting length to 0
// Since Method2 is also pointing to that location, we see the empty array
console.log(Method2); // Output []
nem035
  • 34,790
  • 6
  • 87
  • 99
  • *"In the case of primitives such as strings or numbers, the value is also copied over when a variable is re-assigned so your code would behave as you expect it."* Uh? If `let ArrayList = 42` and `let Method1 = ArrayList; ArrayList = 21;`, then `Method1` is still `42`. Variables always work the same way, it doesn't matter what value they have. Or do you mean something else? I guess I'm confused to which code you are referring to. You also say *"since your dealing with an array this works."*, so it seems you are saying in both sentences that "it works". – Felix Kling Feb 05 '19 at 02:05
  • @FelixKling I wrote this answer late at night and half-brained haha I've removed that "feels wrong but unsure how" description – nem035 Feb 05 '19 at 02:09
  • 1
    I understand :D – Felix Kling Feb 05 '19 at 06:55