A is an array in a JavaScript program.
A = [1,2,3,4];
How to empty that?
By empty we mean Clear/Clean the Array and not delete or remove Array itself in this case A.
Method 1:
//Set A to a new empty array A = [];
Its that simple. This code will set the variable A to a new empty array. This is perfect if you don't have references to the original array A anywhere else because this actually creates a brand new (empty) array. You should be careful with this method because if you have referenced this array from another variable or property, the original array will remain unchanged. Only use this if you only reference the array by its original variable A.
Method 2:
//Set arrat length to zero 0 A.length = 0
Method 3:
//Using Splice method A.splice(0,A.length)
Method 4:
//using while loop with pop method
while(A.length) {
A.pop();
}
No comments :
Post a Comment
Dare to Ask?