r/angularjs • • Sep 23 '22

[Resource] 4 Ways to Empty An Array In JavaScript

https://youtu.be/qA3hbO0R6E4
6 Upvotes

8 comments sorted by

View all comments

Show parent comment

2

u/middlebird Sep 23 '22

Why would anyone do it any other way?

1

u/the_hummus Sep 24 '22

I know you're asking facetiously, but foo.splice(0) is pretty useful because it avoids creating a new object. Useful if you are maintaining multiple references or for memory management.

let a = [1,2,3]
let b = a
b = [] // a is still [1,2,3]

b = a
b.splice(0) // a and b are both [] now