2

So I took a look at the code that controls the counter on the SO advertising page. Then I saw the line where this occured i-->. What does this do?

Here is the full code:

$(function(){

    var visitors = 5373891;
    var updateVisitors = function()
    {
            visitors++;

            var vs = visitors.toString(), 
                 i = Math.floor(vs.length / 3),
                 l = vs.length % 3;
            while (i-->0) if (!(l==0&&i==0))          // <-------- Here it is!!!
                vs = vs.slice(0,i*3+l)
                   + ',' 
                   + vs.slice(i*3+l);
            $('#devCount').text(vs);
            setTimeout(updateVisitors, Math.random()*2000);
    };

    setTimeout(updateVisitors, Math.random()*2000);

});
JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Bob Dylan
  • 4,393
  • 9
  • 40
  • 58
  • 2
    This is obviously copied from http://stackoverflow.com/questions/1642028/what-is-the-name-of-this-operator – Dave O. Jan 09 '10 at 21:05
  • This question is about Javascript. The one you reference is about C/C++. It's true they are similar, and possibly even copied, but I think this is a fair enough variant since it asks about a different language. – Rob Levine Jan 09 '10 at 23:07
  • @Rob Levine: Great!, I'll post a similar question for every programming language that supports both the post decrement operator and the greater than operator :-P (and for every language that supports pre decrement and less than operators xD) – Dave O. Jan 10 '10 at 23:08
  • 1
    http://stackoverflow.com/q/1642028/194544 – beryllium Feb 20 '12 at 13:14

4 Answers4

13

i-->0 is the same as i-- > 0, so the comparison expression if the evaluated value of i-- is greater than 0.

Gumbo
  • 643,351
  • 109
  • 780
  • 844
6

it is not an operator. See this link:

What is the "-->" operator in C++?

var i = 10;

while (i-- > 0)
{
   alert('i = ' + i);
}

Output:

i = 9 
i = 8 
i = 7 
i = 6 
i = 5 
i = 4 
i = 3 
i = 2 
i = 1 
i = 0
Community
  • 1
  • 1
JCasso
  • 5,423
  • 2
  • 28
  • 42
1

Other answers have explained that it's two operators. I'll just add that in the example, it's unnecessary. If you're counting down from a positive integer to zero, you can miss out the greater-than-zero test and your code will be shorter and, I think, clearer:

var i = 10;
while (i--) {
    // Do stuff;
}
Tim Down
  • 318,141
  • 75
  • 454
  • 536
0

Thought of the exact same thread that JCasso thought of. What is the "-->" operator in C++?

I think this code style stems from the early days of programming when terminals had limited display real estate.

Community
  • 1
  • 1
ncremins
  • 9,140
  • 2
  • 25
  • 24
  • No, it's quite a new trend. It's syntactic sugar that reads `while i approaches zero`. Some people like it. Some just gets confused. It really depends on weather you've gotten used to seeing it or not. Kinda like regular expressions: when you first see it you want to vomit violently onto the keyboard, then you grow to love it. – slebetman Jan 09 '10 at 21:04
  • Ahh ok, you learn something new everyday. I'm not a huge fan of it I have to admit i-- > 0 reads better to me. – ncremins Jan 09 '10 at 21:10
  • 1
    I appreciate the `-->` operator, but no number of years of regex use has stopped me vomiting :-) – bobince Jan 10 '10 at 00:27