I < str.length vs str[i]

I < str.length vs str[i]
0.0 0

#1

Will this this have any side effect on my code?
Would it be difficult for people to understand or is it bad practice?

for (var i = 0; i < str.length; i++) //if the value of i is less than the length of str
I know this is the condition typically given but,

for (var i = 0; str[i]; i++) //if str[i] has a value
not only is this shorter, it seems easier to understand.

I’m tempted, but should I not use this?


#2

It works, but just looks like an anti pattern, especially since documentation describes how to define a for loop a particular way. I may avoid it, since it could affect other people’s understanding of your code - especially in an interview setting.


#3

TL;DR: Don’t do that because it can break subtly. See below.

This is why:

const ary = [3,2,1,0,1,2,3];
let sum = 0;

for(let i = 0; ary[i]; i++) {
  sum = sum + ary[i];
}

const realSum = ary.reduce(function(acc, num) {
  return acc + num;
}, 0);

console.log(realSum); // 12
console.log(sum); // 6

realSum == sum; // false

In other words, you may have values in your array that are falsey: null, undefined, 0, ''. Be careful!

Actually, that was probably the worst example. But I’ve been coding all day. The same applies for this:

const ary = [3,2,1,0,1,2,3];
let sum = 0;

for(let i = 0; i < ary.length; i++) {
  sum = sum + ary[i];
}

console.log(sum); // 12

#4

The former is more common. You can also see it in a couple of other languages. If you wanted to use the latter maybe you can use this instead (though I could be wrong) (assuming you only need the characters themselves and you don’t need the indexes in the loop)

for (let char of str) {
  // do something with the `char`s
}

#5

I think it’s a case of being “too clever”. It may work, but doesn’t communicate particularly well. Shorter isn’t necessarily better - more understandable/readable is always better. Consider the person who will inherit your code - in most organizations, you will be maintaining code you didn’t write yourself. So you’re going to want the programmer who wrote it to make it as clear as possible.

Also, generally speaking, you want to cache the length value in a loop … otherwise you are looking it up on every iteration of the loop, rather than doing it once prior to starting the loop.

I almost never use loops at all anymore.

In the case of operating over all of the characters in a string, I’d rather do str.split( ‘’ ) and use an Array method to iterate. Or, if I have to, use a while loop.

But that’s more of a personal preference;


#6

And the docs for for ... in, an approach I actually haven’t tried yet:

It’s worth noting this is ES2015 syntax. If you’re using CodePen, you need to ensure you have Babel selected as a processor for JavaScript I think. If your browser supports ES2015, you should be fine. YMMV.


#7

I see, since the value of ary[3] is 0, it stops the loop.
I would have to write it as ary[i] >= 0 and even that only fixes the 0 problem,
the loop would still end if the value was a negative.

Thanks for the example!