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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:49:07+00:00 2026-06-09T02:49:07+00:00

Possible Duplicate: JavaScript for…in vs for I am using a for loop in my

  • 0

Possible Duplicate:
JavaScript for…in vs for

I am using a for loop in my function. I use both type of loops, “for in” and “for“. Both are producing the same result so I want to know what is the difference between these loops and how to know which loop should be used.

<script type="text/javascript">
var jam = ['amit','sam','ram','soly']
for(var i in jam){
alert(jam[i])
}
//for (i=0; i<jam.length; i++) {
//  jam[i]
//alert(jam[i].length)
//}
</script>
</head>
<body>
</body>
  • 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-06-09T02:49:08+00:00Added an answer on June 9, 2026 at 2:49 am

    It depends on what you need. I usually use for-in loop because it is a shorter form, but sometimes you may want to have control over the iteration variable. For instance, if you want to iterate over even indices, you’d need to use the normal for loop:

    for (var i = 0; i < myarray.length; i+=2) {...}
    

    The same applies, for example, if you want to iterate backwards:

    for (var i = myarray.length-1; i >= 0; i--) {...}
    

    Certainly, for objects, the for-in loop allows you to get the property name in the iteration variable. For example:

    var myobject = {year: 1992, city: "Barcelona"}
    for (var propname in myobject) alert(propname + " = " + myobject[propname]);
    

    In your example, I’d use for-in because you are doing a simple iteration. Moreover I think in a non-optimized Javascript compiler/interpreter, the for-in loop would be even faster because variable increment is done internally (i.e. it is not a Javascript statement such as “i++”).

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

Sidebar

Related Questions

Possible Duplicate: javascript syntax: function calls and using parenthesis t.onclick = doSomething t.onclick =
Possible Duplicate: Query about Html <input type=password> tag…? I want change the character displayed
Possible Duplicate: javascript domready? I want to check whether $(function(){ }); is ready. Return
Possible Duplicate: JavaScript “For …in” with Arrays I looped over various arrays using for-in
Possible Duplicate: What does this mean? (function (x,y)){…}){a,b); in JavaScript Please anyone explain and
Possible Duplicate: What does this mean? (function (x,y)){…}){a,b); in JavaScript I have the following
Possible Duplicate: Javascript for loop and setTimeout issue I want this loop to change
Possible Duplicate: javascript function vs. ( function() { … } ()); Sorry if this
Possible Duplicate: Javascript closure inside loops - simple practical example Javascript: closure of loop?
Possible Duplicate: Javascript that executes after page load script type=text/javascript> function ip(ban, win) {

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.