removeAll vs empty an array with [] in knockoutjs

From the end result point of view there is no difference between the two call, so you will end up with myArray containing no elements.

However there is one small difference (if you don’t care about the different return values):

self.myArray([]);

will replace the underlying array instance with a newly created empty array.

While the

self.myArray.removeAll();

will remove all the items from the underlying array but it will keep the array instance.

So if you have multiple ko.observableArray using the same underlaying array you can see the difference between the two calls:

Demo JSFiddle.

Leave a Comment