A Web Design Community curated by Chris Coyier

Code Snippets Gallery

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 ) => []

3 Responses

  1. ng87 says:

    isnt:

    myArray = [];

    just as good?

  2. CaTaHaC says:

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

  3. Plr store says:

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

Leave a Comment

Remember:
  • Be nice.
  • Wrap multiline code in <pre> and <code> tags and escape it first (turn <'s into &lt;'s).
  • You may use regular HTML stuff like <a href="">, <em>, and <strong>
* This website may or may not contain any actual CSS or Tricks.