How to quickly clear a JavaScript Object?

Well, at the risk of making things too easy…

for (var member in myObject) delete myObject[member];

…would seem to be pretty effective in cleaning the object in one line of code with a minimum of scary brackets. All members will be truly deleted instead of left as garbage.

Obviously if you want to delete the object itself, you’ll still have to do a separate delete() for that.

Leave a Comment