954,597 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How can we delete array in javascript?

Hi all,

I want help from all you wonderful guys. I want to know how can we free memory occupied by an Array. That means all the elements associated with this array should be deleted.

let say:

var bigArray = ["313123", "123123", "sadasd",.........................];


So which will be the best and assured way :

1)

delete bigArray;

OR
2)

bigArray = [];

OR
3) any other ...

Thanks,

Luckychap
Posting Pro in Training
444 posts since Aug 2006
Reputation Points: 83
Solved Threads: 61
 

wow i have created big javascript applications so far and yet i have never needed to delete something from memory. why do you need it?

serkan sendur
Postaholic
Banned
2,062 posts since Jan 2008
Reputation Points: 854
Solved Threads: 127
 

Hi everyone,

Lucky,
here's a simple demo, showing how you can removed items inside your array using the pop(index) method.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title>www.daniweb.com</title>
<script type="text/javascript">
<!--

window.onload = function() {
var bigArray = [];

   for ( var i = 0; i < 500; i++ ) { // creating 500 items into bigArray >>> 
      bigArray[ i ] = "Item" + i;
   } alert("bigArray has a total of " + ( bigArray.length ) + " items inside its collection");
   var aLen = ( bigArray.length - 1 ); 
   for ( var x = aLen; bigArray[ x ]; x-- ) { // Removing all 500 items from the bigArray.
      bigArray.pop( x ); 
   } alert( "\nAll items' from the ( bigArray ) has been removed.\nItem count: " + bigArray.length ); 
};
// --> 
</script>
</head>
<body>
<div></div>
</body>
</html>
essential
Posting Shark
974 posts since Aug 2008
Reputation Points: 114
Solved Threads: 138
 

i dont think he is looking for such a processor consuming way of deleting the memory. i think delete keyword works better for his needs. or basically setting the array to null.

serkan sendur
Postaholic
Banned
2,062 posts since Jan 2008
Reputation Points: 854
Solved Threads: 127
 

Obtaining serkan's adviced of way of freeing the memory and minimized the above application:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title>www.daniweb.com</title>
<script type="text/javascript">
<!--

window.onload = function() {
   var bigArray = [];  
   bigArray2 = [];
   for ( var i = 0; i < 500; i++ ) { // creating 500 items into bigArray >>> 
      bigArray[ i ] = "Item" + i;
      bigArray2[ i ] = "Item" + i;
   } 
/*
  You can use the <strong>delete</strong> operator to delete variables declared implicitly but not those declared with the var statement. 

Output Sample: */

delete bigArray; // ignore's the delete process.

alert( bigArray.length ); // outputs the same length

// either one of the following statement will work, if you have defined it using the var statement.

bigArray = null; // or:

// bigArray = []; // reset's all items...

delete bigArray2; // this one works and delete's the bigArray2 variable.

};
// --> 
</script>
</head>
<body>
<div></div>
</body>
</html>
essential
Posting Shark
974 posts since Aug 2008
Reputation Points: 114
Solved Threads: 138
 

Thanks for taking me into consideration :)

serkan sendur
Postaholic
Banned
2,062 posts since Jan 2008
Reputation Points: 854
Solved Threads: 127
 

Thanks gentlemen,

So, using 'delete' OR '[]' will delete all the items (and memory) associated with the array.

Please confirm this.

Luckychap
Posting Pro in Training
444 posts since Aug 2006
Reputation Points: 83
Solved Threads: 61
 

Hi lucky,

both ways will provide the same results, the only difference between the two procedure, is that when you use the delete operator, your variable will no longer be accessible within the function unless you redefined it.

Hope we've claimed your needs...

@serkan - welcome :), you had a better solution compared to my first post.

essential
Posting Shark
974 posts since Aug 2008
Reputation Points: 114
Solved Threads: 138
 
wow i have created big javascript applications so far and yet i have never needed to delete something from memory. why do you need it?

We usually encounter this problem when working with embedded devices (settop boxes etc.) where there is very less memory. We need to free memory where ever its possible.

Luckychap
Posting Pro in Training
444 posts since Aug 2006
Reputation Points: 83
Solved Threads: 61
 

Appreciated!
Thanks to all.

Luckychap
Posting Pro in Training
444 posts since Aug 2006
Reputation Points: 83
Solved Threads: 61
 

Hello, everybody!

I don't like to dig a pretty old thread. However I must tell you and anyone who will view this thread that:

you should use

delete [] somebigArray;

but not

delete somebigArray;

the later doesn't take effect on the clear of array-type variable.


try this:

<html>
<body>

<script type="text/javascript">
var bigArray= [];
for( var i=0;i<500;i++)
{
bigArray[i]="item"+(i+1);
}
document.write("the length of bigArray is "+bigArray.length+"");
document.write("the items of bigArray are: "+bigArray+"");

delete bigArray;
document.write("the length of bigArray is "+bigArray.length+"");
document.write("the items of bigArray are: "+bigArray+"");

bigArray=[];
document.write("the length of bigArray is "+bigArray.length+"");
document.write("the items of bigArray are: "+bigArray+"");

delete [] bigArray;
document.write("the length of bigArray is "+bigArray.length+"");
document.write("the items of bigArray are: "+bigArray+"");




</script>

</body>
</html>
fansico
Newbie Poster
1 post since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

Neither I nor my browser recognizes delete [] bigArray; as a valid line of javascript.

In which environment does it work Fansico?Airshow

Airshow
WiFi Lounge Lizard
Moderator
2,683 posts since Apr 2009
Reputation Points: 321
Solved Threads: 372
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You