Is it safe to delete an object property while iterating over them?

Viewed 21366

When iterating over an object's properties, is it safe to delete them while in a for-in loop?

For example:

for (var key in obj) {
    if (!obj.hasOwnProperty(key)) continue;

    if (shouldDelete(obj[key])) {
        delete obj[key];
    }
}

In many other languages iterating over an array or dictionary and deleting inside that is unsafe. Is it okay in JS?

(I am using Mozilla's Spidermonkey runtime.)

2 Answers
Related