Empty an Array

This is one of the fastest and easiest ways of emptying an array. Of course there are may other ways, but those usually include creation of a new array. This way you reuse the same array.

var myArray = ["one", "two", "three"];

// console.log( myArray ) => ["one", "two", "three"]

myArray.length = 0;

// console.log( myArray ) => []


  1. User Avatar
    Permalink to comment#


    myArray = [];

    just as good?

    • User Avatar
      Salman Abbas
      Permalink to comment#

      Not if you have references to the array lying around..

  2. User Avatar
    Permalink to comment#

    I bet this:
    myArray.length = 0;
    does not work in all browsers where the snippet provided above – does.

  3. User Avatar
    Plr store
    Permalink to comment#

    Excellent code, myArray.empty() works as well.

    • User Avatar
      Tem Corner
      Permalink to comment#

      it’s not a standard prototype on the Array constructor.
      Atleast not in the latest WebKit browsers.

    • User Avatar
      Permalink to comment#

      not really-doesnt work in some browsers
      myArray=[]; is excellent

  4. User Avatar
    Permalink to comment#

    Ha, just do:

    You can even add that to a prototype as

    Array.prototype.empty() {this.splice(0,this.length);}

    and you are done.

  5. User Avatar
    Permalink to comment#

    I think:

    myArray = [];
    /* or */
    myArray = new Array();

    Would be the best, since it’s cross-browser stuff.

    • User Avatar
      Permalink to comment#

      Make sure you understand what that really means. Your are creating a NEW INSTANCE, and not using the same array. This can lead to a lot of fun debugging if you are not aware of that in cases where references to the original array exist.

    • User Avatar
      Avi Algaly
      Permalink to comment#

      yet again if you have reference then its lost

  6. User Avatar
    Matt W
    Permalink to comment#

    The problem with myArray = [] and myArray = new Array() is that technically you are not emptying the array, but instead you are creating a new array. Creating new arrays is a bit of work for browsers, so simply setting the array’s length to 0 is probably best.

  7. User Avatar

    none of the examples works

    • User Avatar

      Out of the above I’ve only tested emptying an array through splicing and it did work perfectly.

  8. User Avatar
    Mathias Bynens
    Permalink to comment#

    Here’s a jsPerf test case: http://jsperf.com/emptying-arrays

  9. User Avatar
    Permalink to comment#

    Old thread but here are my two cents:

    As the above jsPerf test case shows, creating a new array is FASTER than setting length to 0. The reason for this is that most of the built-in functions and constructors for JS nowadays tend to use native code under the surface to optimize (thanks in no small part to Javascript becoming so popular). This is at least the case in Chrome although I can’t vouch for other browsers.

    In short stick to creating new arrays – old tricks like this aren’t really necessary any more.

    • User Avatar
      Permalink to comment#

      Once again you are missing the point. Creating a new array breaks any references to said array.

  10. User Avatar
    Permalink to comment#

    P.S. Splicing is the worst by far: http://jsperf.com/emptying-arrays/4

  11. User Avatar

    Still there is a flaw in both of your methods.
    A simple example is try with
    var a = [“one”,”two”,”three”];
    a = [];
    a = new Array();
    var a = [“four”,”five”,”six”];
    console.log(a); // Now we expect the out as four*five*six.
    But, output will be ***four*five*six. Which is never a correct procedure. I am looking for a perfect solution, which works on all browsers. Please, let me know if any one of you have one.

    • User Avatar
      Permalink to comment#

      I know it has been over a year since your post, but that snippet worked as expected in Internet Explorer 8 and Firefox 21.

  12. User Avatar
    Harvey Kadyanji
    Permalink to comment#

    This solution works best for me

    var sounds = new Array(“a”,”b”,”c”);

    for(var m=0; m<sounds.length; m++){

    //returns an empty array

Leave a Comment

Posting Code!

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.