JavaScript Empty Array – How to create or empty an Array

JavaScript Empty Array is simply an array with no contents. Empty arrays are used for initializing of an array for later pushing values into it. Values extracted after some calculation or fetched from somewhere needs to be stored somewhere if those values are a set of similar values then they are pushed into an empty Array.

javascript empty array

You can create a JavaScript Empty Array using this following piece of code:

    //Define Empty Array
    let myArr = [];

Now we’ll see how to empty an array already having some values or content. Usually, most of us would do this:

    //Define Array
    let myArr = [1, 2, 3, 4];
    //Create a Function
    function empty(){
        //Redefine as Empty
        list= [];
    }
    //Call the Function
    empty();
This is the most popular method programmers use but there are other ways to empty an array. You can use myArr.length = 0 instead of myArr= [] like this:
    //Define Array
    let myArr = [1, 2, 3, 4];
    //Create a Function
    function empty(){
        //Redefine the Length
        myArr.length=0;
    }
    //Call the Function
    empty();
There is a difference in using both the method, we can define them as
  • myArr = [] does not actually deletes the earlier array, it simply assigns the same variable to a new array reference. The previous contents are still kept in the memory which can lead to memory leaks. Although this is the most popular way, it is not the best.
  • myArr.length = 0 does not refer to anything new, it simply deletes all the contents of that same array. Which means any references to myArr will also get affected. For example, if you have a variable with an array and another reference to that variable then length = 0 will affect both of those variables as they point to the same array. Check this:
a = [1, 2, 3];
a2= a;
Now, a.length = 0 will make the original array zero and the array which a2 is pointing to is the same which is also zero now. If you do a=[] that means a now starts to point to a new empty array reference and a2 is still pointing to the old array [1, 2, 3].
Here is some more example to further clarify your doubts:
    var list= [1, 2, 3];
    var secondList= [1, 2, 3];

    var list2= list;
    var secondList2 = secondList;

    list = [];
    secondList.length = 0;
    
    console.log(list, secondList, list2, secondList2);
    //Output: [] [] [1,2,3] []
Or check this example using functions:
    var list = [1, 2, 3, 4, 5];
    function zeroLength(ar) {
        ar.length = 0;
    }
    function emptyArray(ar) {
        ar = [];
    }

    console.log("Original Length: " + arrayA.length);

    emptyArray(list);
    console.log("After Overwriting: " + list.length);
    
    zeroLength(list);
    console.log("After Using Length: " + list.length);

    //Output:
    //Original Length: 5
    //After Overwriting: 5
    //After Using Length: 0
I hope this article and examples clearly explained to you how to create a JavaScript Empty Array and different methods of emptying a JavaScript array using list = [] and list.length = 0. If you still have any queries let us know in the comments below.

You can further follow the discussion on this topic on StackOverflow.

Write a Comment

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