• How to Empty an Array in JavaScript

    Consider this JavaScript array:

    var arr = [1,2,3,4,5];

    To empty this array, you can use one of the following methods:

    Method 1:

    Reassign the array to an empty array. Here is how to do it.

    arr = []; // reassign array to empty one

    You should use this approach only when there is no reference attached to the existing array. This is also the fastest approach to emptry an array in JavaScript.

    Method 2:

    Reset the length of an array to 0. Here is how to do it.

    arr.length = 0;

    Method 3:

    Using splice() function.

    Here is how to do it.

    arr.splice(0,arr.length);

    This approach will actually return an original array after removing each item from position 0 of an array to the last index.