Incrementing Vars Preferred Method?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RMWChaos
    New Member
    • Oct 2007
    • 137

    Incrementing Vars Preferred Method?

    Yet another inane question...but I am looking for coding "best practices" here.

    I have always used i++ to increment vars, but have recently found that some well-respected JS coders such as Douglas Crawford use a different method i += 1.

    When incrementing by a single integer (1), is the += method still preferred? I can see if you wanted to increment by a value greater than 1 (i.e. i += 2, 3, ... infinity), but otherwise, what is the benefit? Are there situations where i++ can fail or cause problems?

    Thanks.
  • mrhoo
    Contributor
    • Jun 2006
    • 428

    #2
    [CODE=javascript]

    (function(){
    var i= 0,j= 0, tem;
    var A= [],B= [];
    while(i<10){
    A[i]= i+= 1;
    B[j]= j++;
    }
    alert('i+=1 returns: '+A+'\ni++ returns: '+ B);
    })()
    [/CODE]

    Comment

    • RMWChaos
      New Member
      • Oct 2007
      • 137

      #3
      Oh, brilliant!

      Thanks!

      Comment

      Working...