Skip to content

Instantly share code, notes, and snippets.

@pbakondy
Created June 24, 2015 09:16
Show Gist options
  • Save pbakondy/6f6da994bc0e65d0db5e to your computer and use it in GitHub Desktop.
Save pbakondy/6f6da994bc0e65d0db5e to your computer and use it in GitHub Desktop.
How to empty an array
var foo = [1, 2, 3];
var bar = [1, 2, 3];
var foo2 = foo;
var bar2 = bar;
// assigns a reference to a new array to a variable, while any other references are unaffected
foo = [];
// deletes everything in the array, which does hit other references
bar.length = 0;
console.log(foo, bar, foo2, bar2);
// [] [] [1, 2, 3] []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment