Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5847631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:44:44+00:00 2026-05-22T12:44:44+00:00

The javascript for keyword will iterate over all properties of an object. If the

  • 0

The javascript for keyword will iterate over all properties of an object. If the object is modified within the loop body, what happens?

For example, is the following code OK?

for(var key in obj)
    if (whatever(obj[key]))
        delete obj[key];

OK would be if this code works in a deterministic fashion and preferably that all keys in obj are tested exactly once. By contrast, in .NET or Java similar constructs will typically throw an exception.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T12:44:45+00:00Added an answer on May 22, 2026 at 12:44 pm

    I think it works. Just be careful to ask for hasOwnProperty(key) – because for will also happily iterate over inherited properties (and methods, which are just properties with function values).

    Also: http://www.w3schools.com/js/js_loop_for_in.asp says:

    Note: The code in the body of the for…in loop is executed once for each property.

    Also: https://developer.mozilla.org/en/JavaScript/Reference/Statements/for…in says:

    A for…in loop iterates over the properties of an object in an arbitrary order (see the delete operator for more on why one cannot depend on the seeming orderliness of iteration, at least in a cross-browser setting). If a property is modified in one iteration and then visited at a later time, the value exposed by the loop will be its value at that later time. A property which is deleted before it has been visited will not then be visited later. Properties added to the object over which iteration is occurring may either be visited or omitted from iteration. In general it is best not to add, modify, or remove properties from the object during iteration, other than the property currently being visited; there is no guarantee whether or not an added property will be visited, whether a modified property will be visited before or after it is modified, or whether a deleted property will be visited before it is deleted.

    What I read from this is – if you’re modifying values other than the current one, the nondeterminism might bite you in the ass. However, modifying the current one should be okay.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a problem concerning the javascript this keyword when used within a javascript
The following code is not working because default seems to be a JavaScript keyword.
The instanceof keyword in JavaScript can be quite confusing when it is first encountered,
I want to call a google service using javascript with a keyword and a
I need to highlight, case insensitively, given keywords in a JavaScript string. For example:
Javascript delete keyword deletes a named variable slot from nearest execution environment which it
What's the correct syntax to search a .txt file for a keyword in JavaScript?
I'm very confused with JavaScript methods defined in objects and the this keyword. In
Within a web project I am using jQuery and javascript to build a query
I currently have the following javascript code (loosely modeled after how a similar objective

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.