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.
arr = arr + $(this).html();
? If you're adding stuff to your array you should useArray.push()
instead.arr
will be a reference to a string, so from then onarr.length
won't have any effect (aslength
is a read-only property of String).arr = []
will convertarr
back to an empty array at the beginning of each iteration, but then you immediately convert it to a string again. So you either broke your code in trying to simplify it for this example, or you're just doing very weird stuff :P