Home Java Javascript-array JavaScript Array Clear
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

JavaScript Array Clear

Advertisement
Clear() method is used to clear all the defined array. In this section, you will study how to clear the Array in JavaScript.

JavaScript Array Clear

     

Clear() method is used to clear all the defined array. In this section, you will study how to clear the Array in JavaScript. You can see in the given example, we have create an instance of array which consists of elements 'Hello' and 'World'. Then we have used the clear() method which removes all elements from an Array instance. After removing the array by clear() method, the resulting array's length will become zero.

 

 

 

 

 

 

Here is the code:

<html>
<head>
<h2> Array Clear Example</h2>
<script>
var array=new Array();
array[0]="Hello";
array[1]="World";
document.write("<b>Array is: </b> <br> ")
for (var i = 0; i < array.length; i++){
document.write(array[i] + "<BR>");
}
document.write("<b>Now the Array is:</b> <br><br>");
array.clear();
for (var i = 0; i < array.length; i++) {
document.write(array[i]+ "<BR>");
}
</script>
<b>[After using the clear() method, the array is removed.]</b>
</head>
<body bgcolor="lightBlue"/>
</html>

Output will be displayed as:

Download Source Code

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 10, 2008

Related Tutorials

Ask Questions?    Discuss: JavaScript Array Clear  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
John
April 4, 2011
your code is not working

your code is not working, ha ha aa haaaa
Nick
November 16, 2011
Javascript Clear Function

With regards to your Javascript Array Clear post, Javascript does not have a clear() function, therefore this code does not work.
Pierre
June 8, 2012
?

Be sure to type before use : Array.prototype.clear=function() { this.length = 0; }; ....
twisted
September 24, 2012
please revise for clarity

Glad to see you went to the trouble to create something to explain one method of emptying an array. Too bad you decided to name your example array "array" - Never, ever use an example name that is the same as the type of thing you are trying to demonstrate - it's very difficult to figure out what mentions of array refer to the example variable or the command. Experts will know, but they aren't your target audience.
DMCA.com