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 3616076
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:27:17+00:00 2026-05-18T22:27:17+00:00

anyone can explain how to use for…in statement in javascript. I had read the

  • 0

anyone can explain how to use for…in statement in javascript. I had read the w3school article but i think it is not so clear.Below is the code, please explain this:

<html>
<body>
<script type="text/javascript">
var x;
var mycars = new Array();
mycars[10] = "Saab";
mycars[20] = "Volvo";
mycars[30] = "BMW";

for (x in mycars)
{
document.write(mycars[x] + "<br />");
}
</script>
</body>
</html>
  • 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-18T22:27:18+00:00Added an answer on May 18, 2026 at 10:27 pm

    A for in loop will iterate through every property in an object.

    In your example, the x variable will cycle through every property in the mycars object.

    If you add mycars.expensive = "Porsche";, it will find that too.


    Note that, as stated by MDC, for in loops should not be used to loop through ordinary arrays:

    Although it may be tempting to use
    this as a way to iterate over an Array,
    this is a bad idea. The
    for...in statement
    iterates over user-defined properties
    in addition to the array elements, so
    if you modify the array’s non-integer
    or non-positive properties (e.g. by
    adding a "foo" property
    to it or even by adding a method or
    property to
    Array.prototype), the
    for...in statement will
    return the name of your user-defined
    properties in addition to the numeric
    indexes. Also, because order of
    iteration is arbitrary, iterating over
    an array may not visit elements in
    numeric order. Thus it is better to
    use a traditional for loop with a numeric index when
    iterating over arrays. Similar
    arguments might be used against even
    using for…in at all (at least
    without propertyIsEnumerable()
    or hasOwnProperty()
    checks), since it will also iterate
    over Object.prototype (which, though
    usually discouraged, can, as in the
    case of Array.prototype, be usefully
    extended by the user where are no
    namespacing concerns caused by
    inclusion of other libraries which
    might not perform the above checks on
    such iterations and where they are
    aware of the effect such extension
    will have on their own use of
    iterators such as for…in).

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

Sidebar

Related Questions

Can anyone explain the use of ^ operator in java with some examples?
Can anyone explain to me why I would want to use IList over List
Can anyone explaine me, what is MVC, why we use it and how we
Can anyone explain what this mod_rewrite rule is doing? I'm trying to comment the
Can anyone explain what advantages there are to using a tool like MSBuild (or
Can anyone explain why following code won't compile? At least on g++ 4.2.4. And
Can anyone explain in simple words what First and Second Level caching in Hibernate/NHibernate
Can anyone explain the difference between Server.MapPath(.) , Server.MapPath(~) , Server.MapPath(@\) and Server.MapPath(/) ?
Can anyone explain the meaning of and purposes of the values for the SecurityAction
Can anyone explain to me what this means? Run-Time Check Failure #0 - The

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.