Skip to main content
  1. About
  2. For Teams
Asked
Viewed 109 times
4

Im just wondering the difference between these array iterations, and why the second one seems to be really rarely used, is something wrong with it?

var items = [ /*...*/ ]
for (var i = 0; i < items.length; i++) {
    var item = items[i];
    // Do some stuff with the item
}

The second way:

var items = [ /*...*/ ]
for (var i, item; item = items[i]; i++) {
    // Do some stuff with the item
}
2
  • 1
    The difference is that the second won't even run because i hasn't been initialized with a value, unless that's a typo in that case nneonneon is right.
    aziz punjani
    –  aziz punjani
    2012-10-18 18:23:46 +00:00
    Commented Oct 18, 2012 at 18:23
  • 2
    FWIW, ES5 defines an .forEach method for arrays, which seems to be what you would want: developer.mozilla.org/en-US/docs/JavaScript/Reference/….
    Felix Kling
    –  Felix Kling
    2012-10-18 18:29:26 +00:00
    Commented Oct 18, 2012 at 18:29

3 Answers 3

9

The first one is guaranteed to always iterate through every element.

The second one would stop midway if it encounters some false-like element, such as 0.

Sign up to request clarification or add additional context in comments.

3 Comments

I guess you could counteract this by using (item = items[i]) !== undefined but it's a bit confusing and has side effects and ... is ugly.
Yeah...then you're just better off using the first form. It's idiomatic, it's correct, and it's short.
Felix is correct, however, an array could possible have undefined values which might serve a purpose
1

In the second for loop you need to initialize your i variable.

Consider:

var items = ["string", false, null, 0, "", {}, []];

First loop will loop through the entire array. However, second loop's conditional will evaluate the value assigned to item. The boolean value of this portion would be:

!!(item = items[i])

valid values such as null, 0, false, ""(empty string) and undefined will evaluate to false and break. In the array above, you'll break out of the for loop when item is assigned to false

Comments

0

In second case the termination condition is somewhat javascript-specific - loop terminates when assignment converted to boolean yields false, which happens also on reading array element out of its bounds. This is contrary to other popular languages, where out-of-bouns access to array causes some kind of error and does not guarantee the correct condition for terminating for loop. When any programmer with background in other languages than javascript writes iterating loop, he/she probably avoids patters which he knows are incorrect or even dangerous in these languages.

This would be main reason, I think. But other thing is, like mentioned above, that this condition might be false even before reaching end of iterated array - for example when assigned element has value of false, 0, or any other value, which in javascript implicitly converts to false.

Comments

Your Answer

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.