views:

136

answers:

5

What does the following javascript do?

var groups = countrylist.split(',');    
for( var i = -1, group;  group = groupsCounty[++i]; ){
  ...
}
+2  A: 

It's iterating over the elements of groups using the presence of a value in group as the guard condition. i.e.using JavaScript Truthiness to control the number of times the loop iterates, because guard will be false when there is no value that can be assigned to it.

cmsjr
+1  A: 

The for loop walks through the groups array until groupsCounty[++i] returns a falsely value.

With usage of the following terms:

for (<initial-expression>; <condition>; <final-expression>)

The initial-expression var i = -1, group declares the variables i and group. For each iteration the looping condition group = groupsCounty[++i] assignes the next array value to group. If that expression is falsely (e.g. groupsCounty[++i] returns undefined when out of bounds), the loop stops. And the final-expression is empty as i is already increased within the contition expression.

Gumbo
+11  A: 
With i starting at -1
increase i by 1
get the ith element from the groupsCounty array
if there is no such element: stop
otherwise: assign it to group and carry on (with whatever "…" is)

It's an optimised version of:

for (var i = 0; i < groupsCounty.length; i++; ){
    var group = groupsCounty[i];
    …
}
David Dorward
Is it safe to loop in this manner? (as in does it work across all browsers?)
digiguru
Does the question's for-loop actually outperforms the for-loop in this answer? In CPU or in Memory used?
jpabluz
@David, Is that correct? group = groupsCounty[++i] is a bit confusing to me
David Archer
`group = groupsCounty[++i]` is the same as `i = i + 1; group = groupsCounty[i]`
David Dorward
@jpabluz — It saves an operation to check the length each time it goes through the loop. It rarely makes a significant difference, so the cost of readability isn't usually worth it.
David Dorward
@digiguru I don't like this method because you're relying on type coercion to provide an "equivalent" true or false with each iteration. Meaning, if your value in the groupsCounty array is a 0 or "", it will return false. It's also not as clear to just glance at and see what it's doing, to me. Is it safe? Well, it will always do what it's supposed to do, just maybe not what you expect it to do.
Jonathon
yes, I should clarify, I meant the groupsCounty variable is confusing. So assuming groupsCounty is defined, then the answer to "what does this loop achieve?" is nothing really. a local variable named group gets set n times then forgotten??
David Archer
doh, nevermind. Failed to notice group can be used inside the loop. duh me!
David Archer
@Jonathon: +1 its cleverness that just makes the code harder to read.
AnthonyWJones
@Jonathon Thanks for the info. I don't like code that doesn't read easily, so I'm tempted to change it, but I wanted to know what it's doing before I start meddling.
digiguru
A: 

It's equivalent to

for (var group in groupsCounty) {
  ...
}

With the added value of having access to the index (i).

Aistina
`group` would __be__ the index value, you would then have to dereference the array using `anotherVariable = groupdCountry[group]` to aquire the value.
AnthonyWJones
It isn't equivalent. A `for...in` loop enumerates over all properties of the object, including properties inherited from the object's prototype, in an undefined order. So you've got a different loop order and potentially more properties being iterated over. Also, in your example, `group` would be the property name rather than the property value as it is in the original question.
Tim Down
+2  A: 

Its doing this:-

var groups = countrylist.split(',');    
for( var i = 0;  i < groups.length; i++ )
{
  var group = groups[i]
  ...
}

The only real difference is that the above is far more common and more easily recognisable. You would not have posted the above code asking "What is this doing?".

The code you've posted is an example of clever developing but not necessarily good coding practice.

AnthonyWJones