Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Array.length = 0 and Array =[]?

Can some one explain the conceptual difference between both of them. Read somewhere that the second one creates a new array by destroying all references to the existing array and the .length=0 just empties the array. But it didn't work in my case

//Declaration  var arr = new Array(); 

The below one is the looping code that executes again and again.

$("#dummy").load("something.php",function(){    arr.length =0;// expected to empty the array    $("div").each(function(){        arr = arr + $(this).html();    }); }); 

But if I replace the code with arr =[] in place of arr.length=0 it works fine. Can anyone explain what's happening here.

like image 765
Srikanth Rayabhagi Avatar asked Jan 26 '11 11:01

Srikanth Rayabhagi


People also ask

Is array length the same as array length 0?

Since the value of arr. length can only be 0 or larger and since 0 is the only number that evaluates to false , there is no difference.

What is an array of length 0?

Although the size of a zero-length array is zero, an array member of this kind may increase the size of the enclosing type as a result of tail padding. The offset of a zero-length array member from the beginning of the enclosing structure is the same as the offset of an array with one or more elements of the same type.

Does array length count from 0 or 1?

Arrays in JavaScript are zero-based. This means that JavaScript starts counting from zero when it indexes an array. In other words, the index value of the first element in the array is “0” and the index value of the second element is “1”, the third element's index value is “2”, and so on.

What is the difference between array length () and array size ()?

What is the difference between the size of ArrayList and length of Array in Java? ArrayList doesn't have length() method, the size() method of ArrayList provides the number of objects available in the collection. Array has length property which provides the length or capacity of the Array.


1 Answers

foo = [] creates a new array and assigns a reference to it to a variable. Any other references are unaffected and still point to the original array.

foo.length = 0 modifies the array itself. If you access it via a different variable, then you still get the modified array.

Read somewhere that the second one creates a new array by destroying all references to the existing array

That is backwards. It creates a new array and doesn't destroy other references.

var foo = [1,2,3]; var bar = [1,2,3]; var foo2 = foo; var bar2 = bar; foo = []; bar.length = 0; console.log(foo, bar, foo2, bar2); 

gives:

[] [] [1, 2, 3] [] 

arr.length =0;// expected to empty the array 

and it does empty the array, at least the first time. After the first time you do this:

arr = arr + $(this).html(); 

… which overwrites the array with a string.

The length property of a string is read-only, so assigning 0 to it has no effect.

like image 135
Quentin Avatar answered Sep 20 '22 16:09

Quentin