1.

How To Empty An Array In Javascript?

Answer»

For instance,

 var arrayList = ['a','b','c','d','e','f'];

How can we empty the array above?

There are a couple ways we can use to empty an array, so let's discuss them all.

Method 1 :

arrayList = []

Above CODE will SET the variable arrayList to a new empty array. This is recommended if you don't have references to the original array arrayList anywhere else, because it will actually create a new, empty array. You should be careful with this method of emptying the array, because if you have REFERENCED this array from another variable, then the original reference array will remain unchanged.

For Instance,

var arrayList = ['a','b','c','d','e','f']; // Created array 

var anotherArrayList = arrayList; // Referenced arrayList by another variable 

arrayList = []; // Empty the array 

console.log(anotherArrayList); // OUTPUT ['a','b','c','d','e','f']

Method 2 : 

arrayList.length = 0;

The code above will clear the existing array by setting its length to 0. This way of emptying the array also updates all the reference variables that point to the original array. Therefore, this method is useful when you want to update all reference variables pointing to arrayList.

For Instance,

var arrayList = ['a','b','c','d','e','f']; // Created array 

var anotherArrayList = arrayList; // Referenced arrayList by another variable 

arrayList.length = 0; // Empty the array by setting length to 0

console.log(anotherArrayList); // Output []

Method 3 : arrayList.splice(0, arrayList.length);

The implementation above will also work perfectly. This way of emptying the array will also update all the references to the original array.

var arrayList = ['a','b','c','d','e','f']; // Created array 

var anotherArrayList = arrayList; // Referenced arrayList by another variable 

arrayList.splice(0, arrayList.length); // Empty the array by setting length to 0

console.log(anotherArrayList); // Output []

Method 4 :

while(arrayList.length)
{
arrayList.pop();
}

The implementation above can also empty arrays, but it is usually not recommended to use this method often.

For instance,

 var arrayList = ['a','b','c','d','e','f'];

How can we empty the array above?

There are a couple ways we can use to empty an array, so let's discuss them all.

Method 1 :

arrayList = []

Above code will set the variable arrayList to a new empty array. This is recommended if you don't have references to the original array arrayList anywhere else, because it will actually create a new, empty array. You should be careful with this method of emptying the array, because if you have referenced this array from another variable, then the original reference array will remain unchanged.

For Instance,

var arrayList = ['a','b','c','d','e','f']; // Created array 

var anotherArrayList = arrayList; // Referenced arrayList by another variable 

arrayList = []; // Empty the array 

console.log(anotherArrayList); // Output ['a','b','c','d','e','f']

Method 2 : 

arrayList.length = 0;

The code above will clear the existing array by setting its length to 0. This way of emptying the array also updates all the reference variables that point to the original array. Therefore, this method is useful when you want to update all reference variables pointing to arrayList.

For Instance,

var arrayList = ['a','b','c','d','e','f']; // Created array 

var anotherArrayList = arrayList; // Referenced arrayList by another variable 

arrayList.length = 0; // Empty the array by setting length to 0

console.log(anotherArrayList); // Output []

Method 3 : arrayList.splice(0, arrayList.length);

The implementation above will also work perfectly. This way of emptying the array will also update all the references to the original array.

var arrayList = ['a','b','c','d','e','f']; // Created array 

var anotherArrayList = arrayList; // Referenced arrayList by another variable 

arrayList.splice(0, arrayList.length); // Empty the array by setting length to 0

console.log(anotherArrayList); // Output []

Method 4 :

while(arrayList.length)
{
arrayList.pop();
}

The implementation above can also empty arrays, but it is usually not recommended to use this method often.



Discussion

No Comment Found