How To Empty An Array In JavaScript

How To Empty An Array In Javascript

How To Empty An Array In JavaScript

 

How To Empty An Array In JavaScript , In this Article i will show you how to empty an Array  in JavaScript using 2 Methods.

In this Sample Application i will use a few JavaScript function listed below

  1. document.getElementById().innerHTML ,
  2. yourArrayName.push()
  3. yourArrayName.length

To Create This Sample, i will need  few things Setup in HTML

  1. A Text Box for the User to enter the value
  2. A Button for the User to Click ,in order to Store the Array into the Array Variable
  3. A Button for the User to Click , to Check whats inside the Array
  4. A Button for the User to Click to Empty the Array ( 1st Array Empty Method)
  5. A Button for the User to Click to Empty the Array ( 2nd Array Empty Method

I have created  4 Function all together

  1. outArray() -> This Function will print out all element inside the Array
  2. cArray () -> This Function will push the User input Value into the Array
  3. e1Array() -> This Function will empty the Array ( Method 1)
  4. e2Array() -> This Function will empty the Array ( Method2)

The How To Empty An Array In JavaScript Application is as Below

Click the below button to see what is inside our Array Element

Enter an Element you want to create inside the Array :

 

 How To Code This

  1. Create the HTML Structure Doc type,  Head, Body Etc , I attached the code in this Blog , feel free to copy it for your reference
  2. Create the Function outArray() to print out whats inside the Array.
  3. newArray.toString(); , convert the User inputed Array into String
  4. document.getElementById(“outputArray”).innerHTML = newArray; ->This will embed the Value found in the Array to HTML.
  5. Create the Function cArray(createarray)
  6. This Function will push the value , which the User enter into the Array
  7. createarray = document.getElementById(“createArray”).value; -> Get the DOM Value enter by the User  , and save it in  ” createarray”
  8. newArray.push(createarray); -> This will push the Value createarray into the Empty Array called ” newArray “
  9. Create  the function call   e1Array() -> Method 1
  10. This Function will empty the Array
  11. Create  the function call   e2Array() -> Method 2
  12. This Function will empty the Array.

 


<!DOCTYPE html>
<html>
<head>

<script type="text/javascript">
//This is a New Array
var newArray = [];

// This Fuunction Print Out the Array
function outArray() {



newArray.toString();
document.getElementById("outputArray").innerHTML = newArray;


}

// This Fuunction push the enter value into the Array
function cArray(createarray){

createarray = document.getElementById("createArray").value;
newArray.push(createarray);
alert(" After you click this button , Click<Check What inside Button >");


}


// This Fuunction will empty the Array
function e1Array(emptyarray){


newArray = [];
alert(" After you click this button , Click<Check What inside Button Again>");


}

// This Fuunction will empty the Array
function e2Array(emptyarray){


newArray.length = 0;
alert(" After you click this button , Click <Check What inside Button Again>");

}


</script>
</head>
<body>

<h2>Click the below button to see what is inside our Array Element</h2>
<P> <input type="button" value="Check what is inside" id="" onclick="outArray();"/></P>

<p id="outputArray"></p>

<P style="color:blue;"><strong>Enter an Element you want to create inside the Array :</strong><input type="text" style=
"text-align:center; margin-left: 10px;"id="createArray" name="element"
placeholder="Create Array" ><input type="button" style=
"text-align:center; margin-left: 10px;" value="Create an Array" id=""
onclick="cArray(this.createarray);"/></P>



<p><input type="button" style="text-align:center; margin-left: 10px;" value="Empty Array ( Method 1)" id="" onclick="e1Array(this.emptyarray);"/> </P>

<p><input type="button" style="text-align:center; margin-left: 10px;" value="Empty Array ( Method 2)" id="" onclick="e2Array(this.emptyarray);"/> </P>


</body>

</html>

&nbsp;

&nbsp;

&nbsp;

There are tonnes of library and tutorial and explanation on this site , you can check out here

check out JavaScript Bind Method Here 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

3 × three =