Lets say I have created an object and its properties in JavaScript as follows-
var obj = {};
obj['bar'] = 123;
obj['bar']['foo'] = new Array();
obj['bar']['xyz'] = new Array();
After this, I push elements into the two arrays.
If I then write
delete obj['bar'];
Will the two arrays also be deleted ?
They’ll be eligible for garbage collection, assuming nothing else has any references to them. And nothing will, based on that code. When and whether they’re actually cleaned up is up to the implementation.
But note that they’re eligible for GC even before you remove
bar, because your code is doing something quite odd. See the comments:So you didn’t even have to remove
bar, the arrays were instantly eligible for garbage collection. This happens because in JavaScript, numbers are primitives that can be automatically promoted toNumberobjects — but that doesn’t affect the value of the property the primitive number is assigned to. So:If you change your
obj['bar'] =line to:or
…then the
fooandxyzproperties will not instantly disappear, and the arrays will stick around untilbaris cleaned up.